CBSE IP (065) - SQL COMPREHENSIVE PRACTICE SET

31. (i) Write the SQL statement to create a table, Customer with the following specifications:

Table: Customer
Column NameData TypeKey
CIDIntPrimary Key
FNameVarchar (20)
LNameVarchar (20)
AgeInt
+ (i) Write the SQL statement to create the table.
CREATE TABLE Customer ( CID Int PRIMARY KEY, FName Varchar(20), LName Varchar(20), Age Int );
+ (ii) Write the SQL query to display all records in descending order of LName from the Table Customer.
SELECT * FROM Customer ORDER BY LName DESC;

32. (a) Given the following tables:

Table: STUDENTS
S_IDNAMEAGECITY
1Rahul20Delhi
2Priya22Mumbai
3David21Delhi
4Neha23Bengaluru
5Khurshid22Delhi
Table: GRADES
S_IDSUBJECTGRADE
1MathA
2EnglishB
3MathC
4EnglishA
5MathB

Write SQL queries for the following:

+ (i) To display the number of students from each city.
SELECT CITY, COUNT(*) FROM STUDENTS GROUP BY CITY;
+ (ii) To find the average age of all students.
SELECT AVG(AGE) FROM STUDENTS;
+ (iii) To list the names of students and their grades.
SELECT NAME, GRADE FROM STUDENTS, GRADES WHERE STUDENTS.S_ID = GRADES.S_ID;

(b) Consider the following tables:

Table 1: PRODUCTS

This table stores the basic details of the products available in a shop.

PIDPNameCategory
201LaptopElectronics
202ChairFurniture
203DeskFurniture
204SmartphoneNULL
205TabletElectronics
Table 2: SALES

This table records the number of units sold for each product.

SaleIDPIDUnitsSold
30120150
302202100
30320360
30420480
30520570

Write SQL queries for the following:

+ (i) To delete those records from table SALES whose UnitsSold is less than 80.
DELETE FROM SALES WHERE UnitsSold < 80;
+ (ii) To display names of all products whose category is not known.
SELECT PName FROM PRODUCTS WHERE Category IS NULL;
+ (iii) To display the product names along with their corresponding units sold.
SELECT PName, UnitsSold FROM PRODUCTS, SALES WHERE PRODUCTS.PID = SALES.PID;

(b) Ashutosh, who is a manager, has created a database to manage employee records. The database includes a table named EMPLOYEE whose attribute names are mentioned below:

Table : EMPLOYEE
EIDEMP_NAMEDEPTSALARYJOIN_DATE
E01ARJUN SINGHSALES750002019-11-01
E02PRIYA JAINENGINEERING850002020-05-20
E03RAVI SHARMAMARKETING600002018-08-14
E04AYESHANULL500002021-01-10
E05RAHUL VERMAFINANCE400002017-06-25

Write the output of the following SQL Queries:

+ (i) Select SUBSTRING(EMP_NAME, 1, 5) from EMPLOYEE where DEPT = 'ENGINEERING';
PRIYA
+ (ii) Select EMP_NAME from EMPLOYEE where month(JOIN_DATE) = 8;
RAVI SHARMA
+ (iii) Select EMP_NAME from EMPLOYEE where SALARY > 60000;
ARJUN SINGH PRIYA JAIN
+ (iv) Select count(DEPT) from EMPLOYEE;
4

34. (a) An educational institution is maintaining a database for storing the details of courses being offered. The database includes a table COURSE with the following attributes :

Table : COURSE
C_IDC_NAMEINSTRUCTORDURATION
C101Data StructuresDr. Alok40
C102Machine LearningProf. Sunita60
C103Web DevelopmentMs. Sakshi45
C104Database ManagementMr. Suresh50
C105Python ProgrammingDr. Pawan35

Write SQL queries for the following:

+ (i) To add a new record with following specifications : C106, 'Introduction to AI', 'Ms. Preeti', 55.
INSERT INTO COURSE (C_ID, C_NAME, INSTRUCTOR, DURATION) VALUES ('C106', 'Introduction to AI', 'Ms. Preeti', 55);
+ (ii) To display the longest duration among all courses.
SELECT MAX(DURATION) FROM COURSE;
+ (iii) To count total number of courses run by the institution.
SELECT COUNT(*) FROM COURSE;
+ (iv) To display the instructors' name in lower case.
SELECT LOWER(INSTRUCTOR) FROM COURSE;