SQL Revision IP
SQL Revision IP
1. ‘Employee’ table has a column named ‘CITY’ that stores city in which each employee resides. Write SQL query
to display details of all rows except those rows that have CITY as ‘DELHI’ or ‘MUMBAI’ or ‘CHANDIGARH’.
2. Abhay wants to know the number of students who took the test. He writes the following SQL statement to
count STUDENTID without duplicates. However, the statement is not correct. Rewrite the correct
statement.
SELECT DISTINCT (COUNT STUDENTID) FROM RESULTS;
SELECT COUNT (DISTINCT STUDENTID) FROM RESULTS;
3. Aman has used the following SQL command to create a table ‘stu’: [CBSE 2019]
CREATE TABLE stu
(
id INTEGER,
name VARCHAR(100)
);
Then,Aman enters the following SQL statements to enter 3 rows:
INSERT INTO stu VALUES(1,"abc");
INSERT INTO stu VALUES(2,"abc");
INSERT INTO stu VALUES(3,"bcd");
Write the output that will be produced by the following SQL statement :
SELECT name, Count(*)
FROM stu
GROUP BY name;
4. Table student has the columns RNO and SCORE. It has 3 rows in it. Following two SQL
statements were entered that produced the output (AVG(SCORE) as 45 and
COUNT(SCORE) as 2):
(i) AVG(SCORE)
(ii)COUNT(SCORE )
Data in SCORE column is same in two rows. What data is present in the SCORE
column in the three rows ?
5. Consider the following table ‘Transporter’ that stores the order details about items to be transported. Write
SQL commands for the statements (i) to (viii) and write output for SQL queries (ix) and (x).
i) To display names of drivers and destination city where TELEVISION is being transported.
Ans. SELECT DRIVERNAME, DESTINATION FROM TRANSPORTER WHERE ITEM="TELEVISION";
ii) To display driver names and destinations where destination is not MUMBAI.
Ans. SELECT DRIVERNAME, DESTINATION
FROM TRANSPORTER
WHERE DESTINATION <> "MUMBAI";
OR
SELECT DRIVERNAME, DESTINATION
FROM TRANSPORTER WHERE DESTINATION != "MUMBAI";
OR
SELECT DRIVERNAME, DESTINATION
FROM TRANSPORTER
WHERE DESTINATION NOT IN ("MUMBAI");
OR
SELECT DRIVERNAME, DESTINATION
FROM TRANSPORTER
WHERE NOT DESTINATION = "MUMBAI";
iii) To display the names of destination cities where items are being transported.There should be no
duplicate values.
Ans. SELECT DISTINCT(DESTINATION) FROM TRANSPORTER;
iv) To display details of rows that have some value in DRIVERGRADE column.
Ans. SELECT * FROM TRANSPORTER WHERE DRIVERGRADE IS NOT NULL;
v) To display names of drivers, names of items and travel dates for those items that are being transported
on or before 1st April 2019.
Ans. SELECT DRIVERNAME, ITEM, TRAVELDATE FROM TRANSPORTER
WHERE TRAVELDATE <= "2019-04-01";
vi) To display the number of drivers who have ‘MOHAN’ anywhere in their names.
Ans. SELECT COUNT(DRIVERNAME)
FROM TRANSPORTER
WHERE DRIVERNAME LIKE "%MOHAN%" ;
vii) To display the names of drivers, item names and travel dates in alphabetic (ascending) order of driver
names.
Ans. SELECT DRIVERNAME, ITEM, TRAVELDATE FROM TRANSPORTER ORDER BY
DRIVERNAME;
viii) To display names of drivers whose names are three characters long
Ans. SELECT DRIVERNAME FROM TRANSPORTER WHERE DRIVERNAME LIKE "_ _ _";
OR
SELECT DRIVERNAME FROM TRANSPORTER WHERE LENGTH(DRIVERNAME) = 3 ;
ix) SELECT ITEM,COUNT(*) FROM TRANSPORTER GROUP BY ITEM HAVING COUNT(*) > 1;
He now wants to count number of students in each class where the number of students is
more than 3. He has executed the following query :
SELECT MAX (Marks) FROM STUDENT WHERE COUNT (*) > 3 GROUP
BY Class;
But, he got an error. Identify the error(s) and rewrite the query. Also underline the
correction(s) done.
Ans. SELECT CLASS, COUNT(*) FROM STUDENT
GROUP BY CLASS HAVING COUNT (*) > 3;
OR
SELECT COUNT(*) FROM STUDENT
GROUP BY CLASS HAVING COUNT (*) > 3;