My SQL
My SQL
SP19 new
Q1 (a) Which key word is used to sort the records of a table in descending order?
(b) Which clause is used to sort the records of a table?
(c) Which command is used to modify the records of the table?
(d) Which clause is used to remove the duplicating rows of the table?
A1(a) DESC
(b) ORDER BY
(c ) UPDATE
(d) DISTINCT
SP20
Q2 In SQL, name the clause that is used to display the tuples in ascending order
of an attribute. (1)
A2 ORDER BY
Q3 In SQL, what is the use of IS NULL operator? (1)
A3 To check if the column has null value / no value
Q4 Write any one aggregate function used in SQL. (1)
A4 SUM / AVG / COUNT / MAX / MIN
Q5 In SQL, write the query to display the list of tables stored in a database. (1)
A5 SHOW TABLES
SP21 Term2
Q6 Differentiate between char(n) and varchar(n) data types with respect to
databases. (2)
A6 char(n):
- stores a fixed length string between 1 and 255 characters
- if the value is of smaller length, adds blank spaces
- some space is wasted
varchar(n) :
- stores a variable length string
- no blanks are added even if value is of smaller length
- no wastage of space (2)
1
Q7 (i) Which command is used to view the list of tables in a database? (1)
A7 (i) SHOW TABLES; (1)
Q8 (i) A table, ITEM has been created in a database with the following fields:
ITEMCODE, ITEMNAME, QTY, PRICE
Give the SQL command to add a new field, DISCOUNT (of type Integer) to the ITEM
table. (1)
(ii) Categorize following commands into DDL and DML commands?
INSERT INTO, DROP TABLE, ALTER TABLE, UPDATE...SET (2)
A8(i) ALTER TABLE Item
ADD (Discount INT); (1)
(ii) DDL: DROP TABLE, ALTER TABLE
DML: INSERT INTO, UPDATE...SET (2)
Q9 Charu has to create a database named MYEARTH in MYSQL. (3)
She now needs to create a table named CITY in the database to store the records of
various cities across the globe. The table CITY has the following structure:
Table: CITY
2
POPULATION INT,
);
1 mark for correctly creating database.
2 marks for correctly creating the table.
SP22
Q10 Fill in the blank:
______ command is used to remove primary key from the table in SQL.
(a) update (b)remove (c) alter (d)drop (1)
A10 (c) alter (1)
Q11 Which of the following commands will delete the table from MYSQL database?
(a) DELETE TABLE (b) DROP TABLE (c) REMOVE TABLE (d) ALTER TABLE (1)
A11 (b) DROP TABLE (1)
Q12 Fill in the blank:
The SELECT statement when combined with __________ clause, returns records
without repetition.
(a) DESCRIBE (b) UNIQUE (c) DISTINCT (d) NULL (1)
A12 (c) DISTINCT (1)
Q13 Which function is used to display the total number of records from table in a
database?
(a) sum(*) (b) total(*) (c) count(*) (d) return(*) (1)
A13 (c) count(*) (1)
Q14 Differentiate between count() and count(*) functions in SQL with appropriate
example.
A14 COUNT(*) returns the count of all rows in the table, whereas COUNT () is used
with Column_Name passed as argument and counts the number of non-NULL
values in a column that is given as argument.
Example: Table : EMPL
EMPNO ENAME JOB SAL DEPTNO
8369 SMITH CLERK 2985 10
8499 ANYA NULL 9870 20
8566 AMIR SALESMAN 8760 30
8698 BINA MANAGER 5643 20
8912 SUR NULL 3000 10
3
Output
COUNT(*)
5
4
Department: Production (2)
As the primary key is added as the last field, the command for inserting data will be:
INSERT INTO Employee VALUES("Shweta","Production",26900,999);
OR
INSERT INTO Employee(EmpId,Ename,Department,Salary)
VALUES(999,"Shweta","Production",26900); (2)
5
(ii)To display the TNAME and CITY of Trainer who joined the Institute in the month of
December 2001.
(iii)To display TNAME, HIREDATE, CNAME, STARTDATE from tables TRAINER and
COURSE of all those courses whose FEES is less than or equal to 10000.
(iv)To display number of Trainers from each city.
(v)SELECT TID, TNAME, FROM TRAINER WHERE CITY NOT IN(‘DELHI’,
‘MUMBAI’);
(vi)SELECT DISTINCT TID FROM COURSE;
(vii)SELECT TID, COUNT(*), MIN(FEES) FROM COURSE GROUP BY TID HAVING
COUNT(*)>1;
(viii)SELECT COUNT(*), SUM(FEES) FROM COURSE WHERE STARTDATE<
‘2018-09-15’;
A1 (i)SELECT TNAME, CITY, SALARY FROM TRAINER ORDER BY HIREDATE;
(ii)SELECT TNAME, CITY FROM TRAINER WHERE HIREDATE BETWEEN ‘2001-
12-01’ AND ‘2001-12-31’;
OR
SELECT TNAME, CITY FROM TRAINER WHERE HIREDATE >= ‘2001-12-01’ AND
HIREDATE<=‘2001-12-31’;
OR
SELECT TNAME, CITY FROM TRAINER WHERE HIREDATE LIKE ‘2001-12%’;
(iii) SELECT TNAME,HIREDATE,CNAME,STARTDATE FROM TRAINER, COURSE
WHERE TRAINER.TID=COURSE.TID AND FEES<=10000;
SP19 new
Q2 Write a output for SQL queries (i) to (iii), which are based on the table STUDENT
6
given below:
Table : STUDENT
7
A3 (i) SELECT * FROM STUDENT ORDER BY NAME;
(ii) SELECT CLASS,DOB,CITY FROM STUDENT WHERE MARKS
BETWEEN 450 AND 551;
(iii) SELECT NAME,CLASS ,COUNT(*) FROM STUDENT GROUP BY CLASS
HAVING MARKS>450;
(iv) UPDATE STUDENT SET MARKS=MARKS+20 where class=”XII”;
SP19 old
Q4 Write SQL queries for (i) to (iv) and find outputs for SQL queries (v) to (viii), which
are based on the tables.
Table: TRAINER
TID TNAME CITY HIREDATE SALARY
101 SUNAINA MUMBAI 1998-10-15 90000
102 ANAMIKA DELHI 1994-12-24 80000
103 DEEPTI CHANDIGARG 2001-12-21 82000
104 MEENAKSHI DELHI 2002-12-25 78000
105 RICHA MUMBAI 1996-01-12 95000
106 MANIPRABHA CHENNAI 2001-12-12 69000
Table: COURSE
CID CNAME FEES STARTDATE TID
C201 AGDCA 12000 2018-07-02 101
C202 ADCA 15000 2018-07-15 103
C203 DCA 10000 2018-10-01 102
C204 DDTP 9000 2018-09-15 104
C205 DHN 20000 2018-08-01 101
C206 O LEVEL 18000 2018-07-25 105
(i) Display the Trainer Name, City & Salary in descending order of their
Hiredate.
(ii) To display the TNAME and CITY of Trainer who joined the Institute in the
month of December 2001.
(iii) To display TNAME, HIREDATE, CNAME, STARTDATE from tables
TRAINER and COURSE of all those courses whose FEES is less than or
8
equal to 10000.
(iv) To display number of Trainers from each city.
(v) SELECT TID, TNAME, FROM TRAINER WHERE CITY NOT
IN(‘DELHI’, ‘MUMBAI’);
(vi) SELECT DISTINCT TID FROM COURSE;
(vii) SELECT TID, COUNT(*), MIN(FEES) FROM COURSE GROUP BY
TID HAVING COUNT(*)>1;
(viii) SELECT COUNT(*), SUM(FEES) FROM COURSE WHERE
STARTDATE< ‘2018-09-15’;
A4 (i) SELECT TNAME, CITY, SALARY FROM TRAINER ORDER BY HIREDATE;
(ii) SELECT TNAME, CITY FROM TRAINER WHERE HIREDATE
BETWEEN ‘2001-12-01’ AND ‘2001-12-31’;
OR
SELECT TNAME, CITY FROM TRAINER WHERE HIREDATE >=
‘2001-12-01’ AND HIREDATE<=‘2001-12-31’;
OR
SELECT TNAME, CITY FROM TRAINER WHERE HIREDATE LIKE
‘2001-12%’;
(iii) SELECT TNAME,HIREDATE,CNAME,STARTDATE FROM TRAINER, COURSE
WHERE TRAINER.TID=COURSE.TID AND FEES<=10000;
(iv) SELECT CITY, COUNT(*) FROM TRAINER GROUP BY CITY;
(v) TID TNAME
103 DEEPTI
106 MANIPRABHA
(vi) DISTINCT TID
101
103
102
104
105
(vii) TID COUNT(*) MIN(FEES)
9
101 2 12000
(viii) COUNT(*) SUM(FEES)
4 65000
SP20
Q5 Write the outputs of the SQL queries (i) to (iii) based on the relations Teacher
and Posting given below:
Table : Teacher
T_ID Name Age Department Date_of_join Salary Gender
1 Jugal 34 Computer Sc 10/01/2017 12000 M
2 Sharmila 31 History 24/03/2008 20000 F
3 Sandeep 32 Mathematics 12/12/2016 30000 M
4 Sangeeta 35 History 01/07/2015 40000 F
5 Rakesh 42 Mathematics 05/09/2007 25000 M
6 Shyam 50 History 27/06/2008 30000 M
7 Shiv Om 44 Computer Sc 25/02/2017 21000 M
8 Shalakha 33 Mathematics 31/07/2018 20000 F
Table : Posting
P_ID Department Place
1 History Agra
2 Mathematics Raipur
3 Computer Science Delhi
i. SELECT Department, count(*) FROM Teacher
GROUP BY Department;
ii. SELECT Max(Date_of_Join),Min(Date_of_Join)
FROM Teacher;
iii. SELECT Teacher.name, Teacher.Department, Posting.Place FROM Teacher,
Posting WHERE Teacher.Department = Posting.Department AND
Posting.Place=”Delhi”; (3)
A5
OUTPUT:
10
i.
Department Count(*)
History 3
Computer Sc 2
Mathematics 3
ii. Max - 31/07/2018 or 2018-07-31 Min- 05/09/2007 or 2007-09-05
Q6 Write SQL commands for the following queries (i) to (v) based on the
relations Teacher and Posting given below:
Table : Teacher
T_ID Name Age Department Date_of_join Salary Gender
1 Jugal 34 Computer Sc 10/01/2017 12000 M
2 Sharmila 31 History 24/03/2008 20000 F
3 Sandeep 32 Mathematics 12/12/2016 30000 M
4 Sangeeta 35 History 01/07/2015 40000 F
5 Rakesh 42 Mathematics 05/09/2007 25000 M
6 Shyam 50 History 27/06/2008 30000 M
7 Shiv Om 44 Computer Sc 25/02/2017 21000 M
8 Shalakha 33 Mathematics 31/07/2018 20000 F
Table : Posting
P_ID Department Place
1 History Agra
2 Mathematics Raipur
3 Computer Science Delhi
i. To show all information about the teacher of History department. (1)
ii. To list the names of female teachers who are in Mathematics department. (1)
11
iii. To list the names of all teachers with their date of joining in ascending order. (1)
iv. To display teacher’s name, salary, age for male teachers only. (1)
v. To display name, bonus for each teacher where bonus is 10% of salary. (1)
SP21 Term2
Q7 Write the output of the queries (a) to (d) based on the table, Furniture given
below:
Table: FURNITURE
FID NAME DATE OF COST DISCOUNT
PURCHASE
B001 Double Bed 03-Jan-2018 45000 10
T010 Dining Table 10-Mar-2020 51000 5
B004 Single Bed 19-Jul-2021 22000 0
C003 Long Back 30-Dec-2016 12000 3
Chair
T006 Console Table 17-Nov-2019 15000 12
B006 Bunk Bed 01-Jan-2021 28000 14
12
(b) 19-Jul-2021
Q8 Write queries (a) to (d) based on the tables EMPLOYEE and DEPARTMENT
given below: (4)
Table: EMPLOYEE
EMPID NAME DOB DEPTID DESIG SALARY
120 Alisha 23-Jan-1978 D001 Manager 75000
123 Nitin 10-Oct-1977 D002 AO 59000
129 Navjot 12-Jul-1971 D003 Supervisor 40000
130 Jimmy 30-Dec-1980 D004 Sales Rep
131 Faiz 06-Apr-1984 D001 Dep Manager 65000
Table: DEPARTMENT
(b) To display name and respective department name of each employee whose
salary is more than 50000.
(c) To display the names of employees whose salary is not known, in alphabetical
order.
(d) To display DEPTID from the table EMPLOYEE without repetition. (4)
13
(d) SELECT DISTINCT DEPTID FROM EMPLOYEE; (4)
SP22
Q9 (a) Consider the following tables – Bank_Account and Branch:
Table: Bank_Account
ACode Name Type
A01 Amrita Savings
A02 Parthodas Current
A03 Miraben Current
Table: Branch
ACode City
A01 Delhi
A02 Mumbai
A03 Nagpur
14
(i) SELECT DISTINCT TID FROM TECH_COURSE;
(ii) SELECT TID, COUNT(*), MIN(FEES) FROM TECH_COURSE GROUP BY TID
HAVING COUNT(TID)>1;
(iii) SELECT CNAME FROM TECH_COURSE WHERE FEES>15000 ORDER BY
CNAME;
(iv) SELECT AVG(FEES) FROM TECH_COURSE WHERE FEES BETWEEN 15000
AND 17000;
A9 (a)
Acode Name Type City
A01 Amrita Savings Delhi
A01 Amrita Savings Nagpur
A02 Partodas Current Nagpur
(b) (i)
DISTINCT TID
101
NULL
102
104
103
(ii)
TID COUNT(*) MIN(FEES)
101 2 12000
(iii)
CNAME
Digital marketing
Mobile Application Development
(iv) 15500.00
15
( ½ mark for the correct output)
Q10 (a) Write the outputs of the SQL queries (i) to (iv) based on the relations
Teacher and Placement given below: (4)
Table : Teacher
T_ID Name Age Department Date_of_join Salary Gender
1 Arunan 34 Computer 2019-01-10 12000 M
Sc
2 Saman 31 History 2017-03-24 20000 F
3 Randeep 32 Mathematics 2020-12-12 30000 M
4 Samira 35 History 2018-07-01 40000 F
5 Raman 42 Mathematics 2021-09-05 25000 M
6 Shyam 50 History 2019-06-27 30000 M
7 Shiv 44 Computer 2019-02-25 21000 M
Sc
8 Shalakha 33 Mathematics 2018-07-31 20000 F
Table : Placement
P_ID Department Place
1 History Ahmedabad
2 Mathematics Jaipur
3 Computer Nagpur
Sc
(i) SELECT Department, avg(salary) FROM Teacher GROUP BY Department;
(ii) SELECT MAX(Date_of_Join),MIN(Date_of_Join) FROM Teacher;
(iii) SELECT Name, Salary, T.Department, Place FROM Teacher T, Placement P
WHERE T.Department = P.Department AND Salary>20000;
(iv) SELECT Name, Place FROM Teacher T, Placement P
WHERE Gender =’F’ AND T.Department=P.Department;
(b) Write the command to view all tables in a database. (1)
A10 (a) (i)
Department Avg(Salary)
16
Computer Sc 16500.00
History 30000.00
Mathematics 25000.00
(ii)
Max(Date_of_Join) Min(Date_of_Join)
2021-09-05 2017-03-04
(iii)
Name Salary Department Place
Randeep 30000 Mathematics Jaipur
Samira 40000 History Ahemdabad
Raman 25000 Mathematics Jaipur
Shyam 30000 History Ahemdabad
Shiv 21000 Computer Sc Nagpur
SP23
Q11 Consider the table CLUB given below and write the output of the SQL queries
that follow.
CID CNAME AGE GENDER SPORTS PAY DOAPP
5246 AMRITA 35 FEMALE CHESS 900 2006-03-27
4687 SHYAM 37 MALE CRICKET 1300 2004-04-15
1245 MEENA 23 FEMALE VOLLEYBALL 1000 2007-06-18
1622 AMRIT 28 MALE KARATE 1000 2007-09-05
1256 AMINA 36 FEMALE CHESS 1100 2003-08-15
1720 MANJU 33 FEMALE KARATE 1250 2004-04-10
2321 VIRAT 35 MALE CRICKET 1050 2005-04-30
17
(iii) SELECT CNAME, AGE, PAY FROM CLUB WHERE GENDER = "MALE" AND
PAY BETWEEN 1000 AND 1200; (3)
A11 (i) COUNT(DISTINCT SPORTS)
4
(ii) CNAME SPORTS
AMINA CHESS
Based on the given table, write SQL queries for the following:
(i) Increase the salary by 5% of personals whose allowance is known.
(ii) Display Name and Total Salary (sum of Salary and Allowance) of all personals.
The column heading ‘Total Salary’ should also be displayed.
(iii) Delete the record of Supervisors who have salary greater than 25000 (3)
A12
(i) UPDATE Personal
SET Salary=Salary*0.5
WHERE Allowance IS NOT NULL;
18
Table: BRAND
BID BName
M02 Dant Kanti
M03 Medimix
M04 Pepsodent
M05 Dove
A13
(i) SELECT PName, BName FROM PRODUCT P, BRAND B WHERE P.BID=B.BID;
19