0% found this document useful (0 votes)
8 views

SQL Query X

Ii

Uploaded by

suryanshpal52
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

SQL Query X

Ii

Uploaded by

suryanshpal52
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

To CREATE DATABASE

CREATE DATABASE <database name>


mysql>CREATE DATABASE XIIBD
To Display list of DATABASE
mysql>SHOW DATABASES;
To open Database
mysql>USE XIIBD
To Display list of Tables
mysql>SHOW TABLES;
To CREATE Table
mysql>Create Table Student(Stuid Int(4) Primary Key,Sname Varchar(30) Not Null,Roll_No
Int(3) Noll Null Unique, Dob Date Default “2000-01-01”,Joining_Year Year);
mysql>Create Table Marks(Sid Int(4) Primary Key,Hin Int(3) Default 0,Eng Int(3) Default 0,Mat Int(3)
Default 0,Sci Int(3) Default 0,Sst Int(3) Default 0,Total Int(3) Default 0);
To Display the structure of Table
mysql> describe student; OR mysql> desc student;
mysql> describe marks;
To insert row in the table
mysql> INSERT INTO student values(1010,"Avneesh",12,"1996-02-15","2022");
mysql> INSERT INTO student (stuid, sname, roll_no, joining_year) values(1012, "Nitesh
Shukla", 19, "2021");
mysql>INSERT INTO MARKS VALUES (1003, 67,64,44,37,14,0) ,(1004,35, 47, 65 ,54,67,0);
Display Column from the table(Retrieve data from table)
Syntax:- Select col1,col2,col3….. from table name where condition;
Use * in place column name to display All Column.
Display all records of the students and marks
mysql>SELECT * FROM MARKS;
mysql> Select * from student;
Qu1:Display the students’ record those have join after 2020.
mysql>Select * from student where joining_year>2020;
Qu2:Display the students’ record those have join in 2020.
mysql>Select * from student where joining_year=2020;
Qu3:Display the students’ record those have join before 2020.
mysql>Select * from student where joining_year<2020;
Qu4:Display the students’ record those have not join in 2020.
mysql>Select * from student where joining_year<>2020;
mysql>Select * from student where joining_year!=2020;
Qu5:Display the students’ record those have join after 2020 and its student id is 1010.
mysql>Select * from student where joining_year>2020 and stuid=1010;
Qu6: Display the students’ record for student id 1010 or 1001.
mysql>Select * from student where stuid=1001 or stuid=1010;
Qu7: Display the roll numbers of students those have student id 1010 or 1001.
mysql>Select roll_no from student where stuid=1001 or stuid=1010;
Aggregate Functions
Sum(), Min(), Max(), Count(), Avg()
mysql>Select sum(roll_no) from student;
mysql>Select max(roll_no) from student;
mysql>Select min(roll_no) from student;
mysql>Select count(roll_no) from student where joining_year>2020;
mysql>Select count(*) from student where joining_year>2020;
Change Table Header in View
mysql>Select count(*) as "No of Records" from student where joining_year>2020;
mysql>Select sum(roll_no) from student where stuid=1001 or stuid=1002;
In and Not In operator
mysql>Select * from student where stuid in(1001,1002);
mysql>Select * from student where stuid not in(1001,1002);
Qu8:Display marks of the students those have more than 45 in maths and less than 60 in sst.
mysql>SELECT * FROM MARKS WHERE MAT>45 AND SST<60;
Qu9:Add table column average with default value 0 data type decimal.
mysql>ALTER TABLE MARKS ADD AVERAGE DECIMAL(10,3) DEFAULT 0.0;
Qu10:Update total marks of the students by sum of all subjects marks.
mysql>UPDATE MARKS SET TOTAL=HIN+ENG+MAT+SCI+SST;
Qu11:Update average value of marks table by total marks/5.
mysql>UPDATE MARKS SET AVERAGE=TOTAL/5;
Qu12:Remove table column average.
mysql>ALTER TABLE MARKS DROP AVERAGE;
Qu13:Display marks of the students those have more than 45 and less than 60 in maths.
mysql>SELECT * FROM MARKS WHERE MAT between 45 AND 60;
Qu13:Display marks of the students those have not more than 45 and less than 60 in maths.
mysql>SELECT * FROM MARKS WHERE MAT not between 45 AND 60;

Order by: To arrange data in Ascending or Descending order.


Syntax
mysql> select * from stu_data order by <field name> [asc/desc];
Ex:
1. mysql> select * from stu_data order by sname;
2. mysql> select * from stu_data order by fname desc;
Alter table: To change or modify or delete column, datatype or field name in a
table. It is a DDL command.
Syntax
mysql> alter table <table name> add/modify/drop/change <field name> [data type][constraint];
Ex:
1. mysql> alter table stu_data change DOB Date_Of_Birth date default '2000-01-01';
2. mysql>alter table stu_data add Stream varchar(30);
3. mysql>alter table stu_data drop Stream;
4. mysql> alter table stu_data modify Stream varchar(30) default 'Science';
5. mysql> alter table stu_data add Stream char(30) after age;
6. mysql> alter table stu_data modify Stream varchar(30) default 'Science';
7. mysql> alter table stu_data modify Stream varchar(30) default 'Science' after age;
Update: To modify data in a table. It is a DML command.
Syntax
mysql> Update <table name> set <field name>=value [where condition];
Ex:
mysql> Update stu_data set stream="Science";
mysql> Update stu_data set stream="Art" where sname="Reena";
mysql> Update stu_data set stream="Commerce" where sid in (10001, 10007,10010);

QUESTIONS
1. What is a relation? What is the difference between a tuple and an
attribute?
Ans A relation is table having atomic values, unique rows and unordered rows and columns.
A row in a relation is known as tuple whereas a column of a table is known as an attribute .
2. What is primary key in a table?
Ans A Primary Key is a set of one or more attributes that can be uniquely identify tuples
within the relation.
3. What is data redundancy? What are the problems associated with it?
Ans Duplication of data is data redundancy. It leads to the problems like wastage of space
and data inconsistency.
4. Define the following terms: (i) Degree (ii) Cardinality.
Ans (i) Degree: The numbers of attributes (columns) in a relation determine the degree of a
relation.
(ii) Cardinality: The number of tuples (rows) in a relation is called the cardinality of the
relation.
5. What are views? How are they useful?
Ans A view is a virtual table that does not really exist in its own right but it instead derived
from one and more underlying base table(s). The view is kind of table whose contents are
taken upon other tables depending upon a given query condition. No stored file is created to
store contents of a view rather its definition is stored only.
The usefulness of views lies in the fact that they provide an excellent way to give people
access to some but not all of the information in a table.
6. Differentiate between Candidate Key and Primary Key in context of
RBDMS.
Ans Candidate Key. A candidate key is the one that is capable of becoming primary key. i.e.,
a field or attribute that has unique value for each row in the relation.
Primary Key is a designed attribute or a group of attributes whose values can uniquely
identify the tuples in the relation.

7. Differentiate between Candidate key and Alternate key in context of


RDBMS.
Ans Candidate Key. A candidate key is the one that is capable of becoming primary key i.e.,
a field or attribute that has unique value for each row in the relation.
A candidate key that is not a primary key is called an Alternate key.
8. Differentiate between primary key and alternate key.
Ans Primary Key. It is the set of one or more attributes that can uniquely identify tuples
within a relation.
Alternate Key. It is a candidate key which is not primary key.

9. What are candidate keys in a table? Give a suitable example of


candidate keys in a table.
Ans A candidate key is the one that is capable of becoming primary key i., a field or attribute
that has unique value for each row in the relation.
Example Table: ITEM
Ino Item Quantity
101 Pen 560
102 Pencil 340
104 CD 540
10 DVD 200
110 Floppy 400
{ Candidate Keys}
10. Differentiate between Data Definition language and Data Manipulation
language.
Ans The SQL DDL provides commands for defining relation schemas, deleting relationship,
creating indexes and modifying schemas.
The SQL DML includes a query language to insert, delete and modify tuples in the database.
Data Manipulation Language (DML) is used to put values and manipulate them in tables and
other database objects and Data Definition language (DDL) is used to create tables and other
database objects.
11. What is the different between WHERE and HAVING clause?
Ans The HAVING clause places conditions on groups in contrast to WHERE clause, which
places conditions on individual rows.
12. Write one similarity and one difference between CHAR and VARCHAR data types.
Ans:
Similarity:
Both char and varchar can store alphabets as well as numbers. Both can store same type of
values.
Difference:
Char is a fixed length character datatype whereas varchar is a variable length character
datatype.
13. Mrs. Sharma is the classteacher of Class ‘XII A’ She wants to create a table
‘Student’ to store details of her class.
i) Which of the following can be the attributes of Student table?
a) RollNo b) “Amit” c) Name d) 25
ii) Name the Primary key of the table ‘Student’. State reason for choosing it.
Ans:
i) RollNo and Name can be the attributes of student table.
ii) RollNo can become the primary key of the student table as two students cannot have a
same roll number.
QUERY QUESTIONS
1. Write the SQL statement to create EMPLOYEE relation which contains EMPNO, Name, Skill, PayRate.
Ans :-
CREATE TABLE Employee (EmpNo CHAR(4) NOT NULL PRIMARY KEY, Name CHAR(20) NOT
NULL,Skill CHAR(1), PayRate DECIMAL(8,2));
2. Create a table with under mentioned structure (Table name is EMP)
EMPNo NUMBER(4)
DeptNo NUMBER(2)
EmpName CHAR(10)
Job CHAR(10)
Manager NUMBER(4)
Hiredate DATE
Salary NUMBER(7,2) >0
Commission NUMBER(7,2)
Ans :-
CREATE TABLE Emp
( EmpNo Number(4) NOT NULL PRIMARY KEY
DeptNo Number(2),
EmpName Char(10),
Job Char(10),
Manager Number(4),
Hiredate Date,
Salary Decimal(7,2) check (Salary>0),
Commission Decimal(7,2) );

3. Create a table with the under mentioned structure (Table name is DEPT)

DeptNo NUMBER(2)
DeptNam CHAR(12)
Location CHAR(12)
Ans :-
CREATE TABLE Dept
( DeptNo NUMBER(2) NOT NULL PRIMARY KEY,
DeptNam CHAR(12),
Location CHAR(12);
4. Create a table called PROJECT with the columns specified below.
ProjId NUMBER(4)
ProjDesig CHAR(20)
ProjStartDT DATE
ProjEndDT DATE
BudgetAmount NUMBER(7) 0
MaxNoStaff NUMBER(2)
Ans :-
CREATE TABLE Project
( ProjId Number(4) NOT NULL PRIMARY KEY,
ProjDesig Char (20) NOT NULL,
ProjStartDT Date,
ProjEndDT DATE,
BudgetAmount Decimal(7,2) default=0,
MaxNoStaff Number(2) );

5. Create a table called SALGRADE with the columns specified


below:
LowSal NUMBER(7,2)
HighSal NUMBER(7,2)
Grade NUMBER(2)
Ans :-
CREATE TABLE Salgrade (
LowSal Decimal(7,2),
HighSal DECIMAL(7,2),
Grade NUMBER(2) );

6. Insert a record with suitable data in the table EMP, having system date as the Hiredate.
Ans :- Date ( ) function gives the system date.
INSERT INTO Emp VALUES (3008, 18, “XAVIER”, “Manager”, Date( ), 3250, NULL);

7. What is the purpose of key in a table? Give an example of key in a table.


Ans :- A key is used to identify a tuple uniquely with in the relation. The value of key is unique. No rows
in the relation can have same value.
e.g. In an Employee relation EmpCode is a key using EmpCode one can obtain the information of a particular
employee.
8. Write SQL commands for (b) to (e) and write the outputs for (f) on the basis of table GRADUATE.
Table: GRADUATE
S.NO. NAME STIPEND SUBJECT AVERAGE DIV
1 KARAN 400 PHYSICS 68 1
2 DIVAKAR 450 COMPUTER SC 68 1
3 DIVYA 300 CHEMISTRY 62 2
4 ARUN 350 PHYSICS 63 1
5 SABINA 500 MATHEMATICS 70 1
6 JOHN 400 CHEMISTRY 55 2
7 ROBERT 250 PHYSICS 64 1
8 RUBINA 450 MATHEMATICS 68 1
9 VIKAS 500 COMPUTER SC 62 1
10. MOHAN 300 MATHEMATICS 57 2

(a) List the names of those students who have obtained DIV 1 sorted by NAME.
(b) Display a report, listing NAME, STIPEND, SUBJEZCT and amount of stipend received in a
year assuming that the STIPEND is paid every month.
(c) To count the number of students who are either PHYSICS or COMPUTER SC
graduates.
(d) To insert a new row in the GRADUATE table:
11, “KAJOL”, 300, “COMPUTER SC”, 75, 1
(e) Give the output of following SQL statement based on table GRADUATE:
(I) Select MIN(AVERAGE) from GRADUATE where SUBJECT= “PHYSICS”;
(II) Select SUM(STIPEND) from GRADUATE where DIV=2;
(III) Select AVG(STIPEND) from GRADUATE where AVERAGE>=65;
(IV) Select COUNT(distinct SUBJECT) from GRADUATE;
(f) Assume that there is one more table GUIDE in the database as shown below:
Table: GUIDE
MAINAREA ADVISOR
PHYSICS VINOD
COMPUTER SC ALOK
CHEMISTRY RAJAN
MATHEMATICS MAHESH
What will be the output of the following query?
SELECT NAME, ADVISOR FROM GRADUATE, GUIDE WHERE SUBJECT =” MAINAREA”
Ans:-
(a) Select Name From GRADUATE Where DIV = 1 Order by Name;
(b) Select Name, stipend, subject, stepend * 12 From GRADUATE
(c) Select count (*) From GRADUATE Where subject IN (“PHYSICS”, “COMPUTER SC”);
(d) Insert into GRADUATE Values (11, “KAJOL”, 300, “COMPUTER SC”, 75, 1);
(e) (i) 63 (ii) 1000 (iii) 450(iv) 4
(f)
KARAN VINOD
DIVAKAR ALOK
DIVYA RAJAN
ARUN VINOD
SABINA MAHESH
JOHN RAJAN
ROBERT VINOD
RUBINA MAHESH
VIKAS ALOK
MOHAN MAHESH
9. “ABC” Event Management Company requires data of events that are to be organized. Write
SQL query to create a table ‘Event’ with the following structure :

Ans: Create table Event(EventID Int Primary Key,Event Varchar(50),DateEvent Date,NumPerformers Int);
10. Suggest her suitable command for the following purpose:
I. To display the list of the database already existing in MySQL.
II. To use the database named City.
III. To remove the pre-existing database named Clients.
IV. To remove all the records of the table named “Club” at one go along with its structure
permanently.
Ans :
i. Show databases;
ii. Use City;
iii. Drop database Clients;
iv. Drop table Club;

You might also like