Unit 3- Database Management
Unit 3- Database Management
CBSE SYLLABUS
1. Database Concepts
Introduction
Data Definition Language (DDL) & Data Manipulation Language (DML)
Data Types: char(n), varchar(n), int, float, date
Constraints: not null, unique, primary key
Database Commands:
o CREATE DATABASE, USE DATABASE, SHOW DATABASES, DROP DATABASE, SHOW TABLES
o CREATE TABLE, DESCRIBE TABLE, ALTER TABLE (add/remove an attribute, add/remove primary
key), DROP TABLE
Data Manipulation Commands:
o INSERT, DELETE, SELECT
Operators:
o Mathematical, Relational, Logical
Clauses and Functions:
o Aliasing, DISTINCT Clause, WHERE Clause
o IN, BETWEEN, ORDER BY
o Meaning of NULL, IS NULL, IS NOT NULL, LIKE
o UPDATE Command, DELETE Command
o Aggregate Functions: MAX, MIN, AVG, SUM, COUNT
o GROUP BY, HAVING Clause
Joins in SQL:
o Cartesian Product on Two Tables
o Equi-Join and Natural Join
THEORY
1. Database Concepts
Database: A structured collection of data that allows efficient storage, retrieval, and management.
Need for Database:
o Organizes data efficiently
o Reduces redundancy
o Ensures data integrity
o Supports multiple users
Page 1 of 34
Example: A school database stores student records, teacher details, and exam results.
DESCRIBE Student;
Page 2 of 34
ALTER TABLE Student DROP COLUMN Address;
Operators in SQL
Operator Type Example Usage
Mathematical +, -, *, / SELECT Age+1 FROM Student;
Relational =, !=, >, < SELECT * FROM Student WHERE Age > 15;
Logical AND, OR, NOT SELECT * FROM Student WHERE Age > 15 AND Class='10A';
NULL Handling
SELECT * FROM Student WHERE Address IS NULL;
SELECT * FROM Student WHERE Address IS NOT NULL;
Aggregate Functions
SELECT MAX(Age), MIN(Age), AVG(Age), SUM(Age), COUNT(*) FROM Student;
Joins in SQL
Join Type Example Description
Cartesian SELECT * FROM Student, Marks; Returns all possible
Join combinations
Equi-Join SELECT Student.Name, Marks.Subject FROM Student Matches common values
INNER JOIN Marks ON Student.RollNo = Marks.RollNo;
Natural SELECT * FROM Student NATURAL JOIN Marks; Automatically joins using
Join common columns
Page 3 of 34
Interface of Python with SQL Database
Connecting SQL with Python
import mysql.connector
# Establish Connection
conn = mysql.connector.connect(host="localhost", user="root", password="1234", database="SchoolDB")
cursor = conn.cursor()
Executing Queries in Python
Insert Data
cursor.execute("INSERT INTO Student (RollNo, Name, Age, Class) VALUES (102, 'Riya', 17, '12A')")
conn.commit()
Update Data
cursor.execute("UPDATE Student SET Age = 18 WHERE RollNo = 102")
conn.commit()
Delete Data
cursor.execute("DELETE FROM Student WHERE RollNo = 102")
conn.commit()
cursor.execute("INSERT INTO Student (RollNo, Name, Age, Class) VALUES (%s, %s, %s, %s)",
(roll_no, name, age, class_name))
conn.commit()
Page 5 of 34
3. DML
4. TCL
Answer: 2. QAL
Question 11
Which of the following attributes can be considered as a choice for primary key?
1. Name
2. Street
3. Roll No
4. Subject
Answer: 3. Roll No
Question 12
What is the full form of SQL?
1. Structured Query Language
2. Structured Query List
3. Simple Query Language
4. None of these
Answer: 1. Structured Query Language
Question 13
What is the full form of DDL?
1. Dynamic Data Language
2. Detailed Data Language
3. Data Definition Language
4. Data Derivation Language
Answer: 3. Data Definition Language
Question 14
What does DML stand for?
1. Different Mode Level
2. Data Model Language
3. Data Mode Lane
4. Data Manipulation Language
Answer: 4. Data Manipulation Language
Question 15
Which is the subset of SQL commands used to manipulate database structures, including tables?
1. Data Definition Language (DDL)
2. Data Manipulation Language (DML)
3. Both (1) and (2)
4. None of these
Answer: 1. Data Definition Language (DDL)
Question 16
Which of the following sublanguages of SQL is used to define the structure of the relation, deleting
relations, and relating schemas?
1. DML (Data Manipulation Language)
2. DDL (Data Definition Language)
3. Query
4. Relational Schema
Answer: 2. DDL (Data Definition Language)
Question 17
Which of the following sublanguages of SQL is used to query information from the database and to insert
tuples into, delete tuples from, and modify tuples in the database?
1. DML (Data Manipulation Language)
2. DDL (Data Definition Language)
3. Query
4. Relational Schema
Answer: 1. DML (Data Manipulation Language)
Page 6 of 34
Question 18
Consider the following SQL statement. What type of statement is this?
SELECT * FROM employee
1.DML
2.DDL
3.DCL
4.Integrity constraint
Answer: 1. DML
Question 19
Which of the following keywords will you use in the following query to display the unique values of the
column dept_name?
SELECT ............... dept_name FROM Company;
1. All
2. From
3. Distinct
4. Name
Answer: 3. Distinct
Question 20
Which of the following keywords will you use in the following query to display all the values of the column
dept_name?
SELECT ............... dept_name FROM Company;
1. All
2. From
3. Distinct
4. Name
Answer: 1. All
Question 21
The ............... clause of the SELECT query allows us to select only those rows in the result that satisfy a
specified condition.
1. Where
2. From
3. Having
4. Like
Answer: 1. Where
Question 22
............... clause of the following query must be added with keyword ............... to display the fields given in
the select list as per a given condition.
SELECT ID, name, dept name, salary * 1.1
WHERE instructor = 1005;
1.where, having
2.select, from
3.where, from
4.where, select
Answer: 3. where, from
Question 23
Which of the following queries contains an error?
1. SELECT * FROM emp WHERE empid = 10003;
2. SELECT empid FROM emp WHERE empid = 10006;
3. SELECT empid FROM emp;
4. SELECT empid WHERE empid = 1009 AND lastname = 'GUPTA';
Answer: 4. SELECT empid WHERE empid = 1009 AND lastname = 'GUPTA';
Question 24
Which of the names will not be displayed by the below-given query?
SELECT name FROM employee WHERE employee_id > 1009;
1. Misha, Khushi
2. Khushi, Japneet
Page 7 of 34
3. Japneet
4. Misha, Japneet
Answer: 1. Misha, Khushi
Question 25
Which operator performs pattern matching?
1. BETWEEN operator
2. LIKE operator
3. EXISTS operator
4. None of these
Answer: 2. LIKE operator
Question 26
Which one of the following has to be added into the blank space to select the subject which has "Computer
Science" as its ending string?
SELECT name FROM class WHERE subject LIKE ' ............... Computer Science';
1. $
2. _
3. ||
4. %
Answer: 4. %
Question 27
Which operator tests a column for the absence of data (i.e., NULL value)?
1. EXISTS operator
2. NOT operator
3. IS operator
4. None of these
Answer: 3. IS operator
Question 28
The pattern _ _ _ matches any string of ............... three characters. _ _ _% matches any string of ...............
three characters.
1. At least, Exactly
2. Exactly, At least
3. At least, All
4. All, Exactly
Answer: 2. Exactly, At least
Question 29
By default, ORDER BY clause lists the results in ............... order.
1. Descending
2. Any
3. Same
4. Ascending
Answer: 4. Ascending
Question 30
To display the salary from greater to smaller and name in alphabetical order, which of the following options
should be used?
Answer: 3. Desc, Asc
FILL IN THE BLANKS
1. Collection of logically related data tables is called a database.
2. The duplication of data is known as data redundancy.
3. A pool of values wherefrom a field can draw values, is called domain.
4. A row in a relation is called a tuple.
5. A column in a relation is called an attribute.
6. The number of attributes in a relation is called its degree.
7. The number of tuples in a relation is called its cardinality.
Page 8 of 34
8. An attribute that can uniquely identify each tuple in a relation is called primary key.
9. A non-key attribute derived from the primary key of some other relation is called foreign key.
10. A data model wherein data is arranged in tabular forms called relations and linked through common
attributes of relations, is called relational data model.
11. Collection of logically related data tables is called a database.
12. The duplication of data is known as data redundancy.
13. A pool of values wherefrom a field can draw values, is called domain.
14. A row in a relation is called a tuple.
15. A column in a relation is called an attribute.
16. The number of attributes in a relation is called its degree.
17. The number of tuples in a relation is called its cardinality.
18. An attribute that can uniquely identify each tuple in a relation is called primary key.
19. A non-key attribute derived from the primary key of some other relation is called foreign key.
20. A data model wherein data is arranged in tabular forms called relations and linked through common
attributes of relations, is called relational data model.
21. SQL stands for Structured Query Language.
22. The SQL keyword FROM is used to specify the table(s) that contains the data to be retrieved.
23. To remove duplicate rows from the result of a query, specify the SQL qualifier DISTINCT in the
select list.
24. To obtain all columns, use a(n) asterisk (*) instead of listing all the column names in the select list.
25. The SQL WHERE clause contains the condition that specifies which rows are to be selected.
26. To sort the rows of the result table, the ORDER BY clause is specified.
27. Columns can be sorted in descending sequence by using the SQL keyword DESC.
28. When two conditions must both be true for the rows to be selected, the conditions are separated by
the SQL keyword AND.
29. The SQL keyword LIKE is used in SQL expressions to select based on patterns.
30. By default, ORDER BY clause lists the records in ascending order.
ASSERTION
1. Assertion: A database is centrally stored data, and a DBMS is a system to manage the database.
Reason: DBMS is a database management system, which is software managing the databases.
Answer: (a) Both assertion and reason are correct, and the reason is a correct explanation of the
assertion.
2. Assertion: Data redundancy may lead to data inconsistency.
Reason: When redundant data or multiple copies of data mismatch, it makes the data inconsistent.
Answer: (a) Both assertion and reason are correct, and the reason is a correct explanation of the
assertion.
3. Assertion: Data redundancy may lead to many problems.
Reason: In RDBMS, data redundancy is 100% removed.
Answer: (c) The assertion is correct, but the reason is incorrect.
4. Assertion: A primary key is used to uniquely identify the rows in a data table.
Reason: A primary key is a field or attribute that has a unique value for each row or tuple.
Answer: (a) Both assertion and reason are correct, and the reason is a correct explanation of the
assertion.
5. Assertion: A data table can have only one primary key.
Reason: In a data table, there can be only one attribute/field containing unique values for each row.
Answer: (b) Both assertion and reason are correct, but the reason is not a correct explanation of the
assertion.
6. Assertion: There can be multiple options for choosing a primary key in a data table.
Reason: All attribute combinations inside a data table that contain unique values for each row are the
candidate keys.
Answer: (a) Both assertion and reason are correct, and the reason is a correct explanation of the
assertion.
Page 9 of 34
7. Assertion: All types of keys contain unique values for each row.
Reason: A foreign-key attribute of a table is the primary key of another table.
Answer: (c) The assertion is correct, but the reason is incorrect.
8. Assertion: The foreign keys of tables are used to establish relationships with other tables and must
be handled carefully.
Reason: Referential integrity is a system of rules that a DBMS uses to ensure that the relationships
between tables remain valid and no accidental change or deletion occurs in the related data.
Answer: (a) Both assertion and reason are correct, and the reason is a correct explanation of the
assertion.
9. Assertion: A unique value that identifies each row uniquely is the primary key.
Reason: Only one column can be made the primary key.
Answer: (c) The assertion is correct, but the reason is incorrect.
10. Assertion: A database is centrally stored data, and a DBMS is a system to manage the database.
Reason: DBMS is a database management system, which is software managing the databases.
Answer: (a) Both assertion and reason are correct, and the reason is a correct explanation of the
assertion.
11. Assertion: Data redundancy may lead to data inconsistency.
Reason: When redundant data or multiple copies of data mismatch, it makes the data inconsistent.
Answer: (a) Both assertion and reason are correct, and the reason is a correct explanation of the
assertion.
12. Assertion: Data redundancy may lead to many problems.
Reason: In RDBMS, data redundancy is 100% removed.
Answer: (c) The assertion is correct, but the reason is incorrect.
13. Assertion: A primary key is used to uniquely identify the rows in a data table.
Reason: A primary key is a field or attribute that has a unique value for each row or tuple.
Answer: (a) Both assertion and reason are correct, and the reason is a correct explanation of the
assertion.
14. Assertion: A data table can have only one primary key.
Reason: In a data table, there can be only one attribute/field containing unique values for each row.
Answer: (b) Both assertion and reason are correct, but the reason is not a correct explanation of the
assertion.
15. Assertion: There can be multiple options for choosing a primary key in a data table.
Reason: All attribute combinations inside a data table that contain unique values for each row are the
candidate keys.
Answer: (a) Both assertion and reason are correct, and the reason is a correct explanation of the
assertion.
16. Assertion: All types of keys contain unique values for each row.
Reason: A foreign-key attribute of a table is the primary key of another table.
Answer: (c) The assertion is correct, but the reason is incorrect.
17. Assertion: The foreign keys of tables are used to establish relationships with other tables and must
be handled carefully.
Reason: Referential integrity is a system of rules that a DBMS uses to ensure that the relationships
between tables remain valid and no accidental change or deletion occurs in the related data.
Answer: (a) Both assertion and reason are correct, and the reason is a correct explanation of the
assertion.
18. Assertion: A unique value that identifies each row uniquely is the primary key.
Reason: Only one column can be made the primary key.
Answer: (c) The assertion is correct, but the reason is incorrect.
Page 10 of 34
Question 1
Observe the following table MEMBER carefully and write the name of the RDBMS operation out of:
(i) SELECTION
(ii) PROJECTION
(iii) UNION
(iv) CARTESIAN PRODUCT,
which has been used to produce the output as shown in RESULT. Also, find the Degree and Cardinality of
the RESULT table.
MEMBER Table
NO MNAME STREAM
M001 JAYA SCIENCE
M002 ADITA HUMANITIES
M003 HANSRAJ SCIENCE
M004 SHIVAK COMMERCE
RESULT Table
NO MNAME STREAM
M002 ADITYA HUMANITIES
Answer
The RDBMS operation used to produce the RESULT table is SELECTION, because the output
table contains only one row that meets a specific condition (e.g., selection based on NO = 'M002').
Degree (Number of Attributes): 3 (NO, MNAME, STREAM)
Cardinality (Number of Rows): 1 (only one row in the RESULT table)
Question 2
Write SQL queries for (i) to (iv) and find outputs for SQL queries (v) to (viii), which are based on the tables
DVD and MEMBER.
Tables Given:
DVD Table
DCODE DTITLE DTYPE
F101 Henry Martin Folk
C102 Dhrupad Classical
C101 The Planets Classical
F102 Universal Soldier Folk
R102 A Day in Life Rock
MEMBER Table
MID NAME DCODE ISSUEDATE
101 AGAM SINGH R102 2017-11-30
103 ARTH JOSEPH F102 2016-12-13
102 NISHA HANS C101 2017-07-24
(i) To display all details from the table MEMBER in descending order of ISSUEDATE.
SELECT * FROM MEMBER ORDER BY ISSUEDATE DESC;
Output:
Page 11 of 34
MID NAME DCODE ISSUEDATE
101 AGAM SINGH R102 2017-11-30
102 NISHA HANS C101 2017-07-24
103 ARTH JOSEPH F102 2016-12-13
(ii) To display the DCODE and DTITLE of all Folk Type DVDs from the table DVD.
SELECT DCODE, DTITLE FROM DVD WHERE DTYPE = 'Folk';
(iii) To display the DTYPE and number of DVDs in each DTYPE from the table DVD.
SELECT DTYPE, COUNT(*) AS Total_DVDs FROM DVD GROUP BY DTYPE;
(iv) To display all NAME and ISSUEDATE of those members from the table MEMBER
who have DVDs issued (i.e., ISSUEDATE) in the year 2017.
SELECT NAME, ISSUEDATE FROM MEMBER WHERE YEAR(ISSUEDATE) = 2017;
Question:3
Give a suitable example of a table with sample data and illustrate Primary and Candidate keys.
Explanation:
Candidate Keys:
o RollNo (Unique for every student)
o Email (Every student has a unique email)
o Phone (Every student has a unique phone number)
Primary Key (Chosen from Candidate Keys):
o RollNo (Most suitable as it is a simple and numeric identifier)
Thus, RollNo is the Primary Key, while Email and Phone are Candidate Keys.
Page 12 of 34
Question:4
A table ‘customer’ has 10 columns but no row. Later, 10 new rows are inserted and 3 rows are deleted
in the table. What is the degree and cardinality of the table ‘customer’?
Answer:
In the context of relational databases:
Degree (Attributes/Columns): The number of columns in a table is called the degree.
Cardinality (Rows/Tuples): The number of rows in a table is called the cardinality.
Given Data:
Initially, the table customer has 10 columns and 0 rows.
10 new rows are inserted.
3 rows are deleted.
Final Calculation:
Degree = 10 (as the number of columns remains the same).
Cardinality = 10 - 3 = 7 (as 3 rows were deleted from the 10 inserted rows).
Final Answer:
Degree = 10
Cardinality = 7
Question 5
Given tables:
Table: VEHICLE
VCODE VEHICLETYPE PERKM
V01 VOLVO BUS 150
V02 AC DELUXE 125
V03 ORDINARY BUS 80
V05 SUN 30
V04 CAR 18
Table: TRAVEL
CNO CNAME TRAVELDATE KM VCODE NOP
101 K. Niwal 2015-12-13 200 V01 32
103 Fredrick Sym 2016-03-21 120 V03 45
105 Hitesh Jain 2016-04-23 450 V02 42
102 Ravi Anish 2016-01-13 80 V02 40
107 John Malina 2015-02-10 65 V04 2
104 Sahanubhuti 2016-01-28 90 V05 4
106 Ramesh Jaya 2016-04-06 100 V01 25
Page 13 of 34
CNO CNAME TRAVELDATE
104 Sahanubhuti 2016-01-28
103 Fredrick Sym 2016-03-21
102 Ravi Anish 2016-01-13
101 K. Niwal 2015-12-13
(ii) To display the CNAME of all customers from the table TRAVEL who are travelling by vehicle with
code 'V01' or 'V02'.
SELECT CNAME
FROM TRAVEL
WHERE VCODE IN ('V01', 'V02');
✅ Output:
CNAME
K. Niwal
Hitesh Jain
Ravi Anish
Ramesh Jaya
(iii) To display the CNO and CNAME of those customers from the table TRAVEL who travelled between
'2015-12-31' and '2015-05-01'.
SELECT CNO, CNAME
FROM TRAVEL
WHERE TRAVELDATE BETWEEN '2015-05-01' AND '2015-12-31';
✅ Output:
CNO CNAME
101 K. Niwal
(iv) To display all the details from table TRAVEL for the customers, who have travelled a distance of
more than 120 KM, in ascending order of NOP.
SELECT *
FROM TRAVEL
WHERE KM > 120
ORDER BY NOP ASC;
✅ Output:
CNO CNAME TRAVELDATE KM VCODE NOP
101 K. Niwal 2015-12-13 200 V01 32
105 Hitesh Jain 2016-04-23 450 V02 42
(v) Find the count of each VCODE where count is greater than 1.
SELECT COUNT(*) AS Total, VCODE
FROM TRAVEL
GROUP BY VCODE
HAVING COUNT(*) > 1;
✅ Output:
Total VCODE
2 V01
2 V02
(vii) To display VCODE, CNAME, and VEHICLETYPE from TRAVEL and VEHICLE tables for those who
traveled less than 90 KM.
✅ Output:
(viii) To display CNAME and total fare (KM * PERKM) for all customers except those traveling by 'V05'.
✅ Output:
CNAME TotalFare
K. Niwal 30000
Page 15 of 34
Question:6
Consider the following tables SCHOOL and ADMIN and answer this question : Give the output the
following SQL queries :
1. Select Designation Count (*) From Admin Group By Designation Having Count (*) <2;
ANSWER-
Page 16 of 34
Question:7
Write SQL qureries for (i) to (iv) and find outputs for SQL queries (v) to (viii), which are based on the tables
TRANSPORT and TRIP
ANSWER-
Page 17 of 34
Question:8
Write SQL query to add a column total price with datatype numeric and size 10, 2 in a table product.
ANSWER-
ALTER TABLE PRODUCT ADD TOTAL PRICE NUMBER (10,2).
Question:9
Differentiate between DELETE and DROP table commands ?
ANSWER-
Question:10
Write SQL commands for the queries (i) to (iv) and output for (v) & (viii) based on a table COMPANY and
CUSTOMER.
1. To display those company name which are having prize less than 30000.
3. To increase the prize by 1000 for those customer whose name starts with „S?
4. To add one more column totalprice with decimal] 10,2) to the table
Page 18 of 34
5. SELECT COUNT(*) , CITY FROM COMPANY GROUP BY CITY;
Question:11
Consider the following tables SCHOOL and ADMIN and answer
1. To display TEACHERNAME, PERIODS of all teachers whose periods are more than 25.
2. To display all the information from the table SCHOOL in descending order of experience.
4. To display TEACHERNAME, CODE and corresponding DESIGNATION from tables SCHOOL and
ADMIN of Male teachers.
Page 19 of 34
ANSWER-
Question:12
Write SQL commands for the queries (i) to (iv) and output for (v) to (viii) based on the tables Watches’ and
Sale given below.
1. TO DISPLAY ALL THE DETAILS OF THOSE WATCHES WHOSE NAME ENDS WITH ‘TIME’
2. TO DISPLAY WATCH’S NAME AND PRICE OF THOSE WATCHES WHICH HAVE PRICE RANGE
IN BE-TWEEN 5000-15000.
ANSWER-
1. SELECT * FROM WATCHES WHERE WATCH_NAME LIKE ‘%TIME’
2. SELECT WATCH_NAME, PRICE WATCH WHERE PRICE BETWEEN 5000 AND 15000;
Page 20 of 34
3. SELECT SUM (QTY STORE) FROM WATCHES WHERE TYPE LIKE ‘UNISEX’;
4. SELECT WATCHNAME, QTY SOLD FROM WATCHES W,SALE S WHERE W. WATCHED = S.
WATCHED AND QUARTER = 1;
Question:13
Answer the questions (a) and (b) on the basis of the following tables SHOP and ACCESSORIES.
ANSWER-
(a)
Question:14
Write SQL queries for (a) to (f) and write the outputs for the SQL queries mentioned shown in (gl) to (g4)
parts on the basis of tables PRODUCTS and SUPPLIERS
Page 22 of 34
1. To display the details of all the products in ascending order of product names (i.e., PNAME).
2. To display product name and price of all those products, whose price is in the range of 10000 and 15000
(both values inclusive).
3. To display the number of products, which are supplied by each suplier. i.e., the expected output should be;
S01 2 S02 2 S03 1
4. To display the price, product name and quantity (i.e., qty) of those products which have quantity more thhn
100.
5. To display the names of those suppliers, who are either from DELHI or from CHENNAI.
6. To display the name of the companies and the name of the products in descending order of company names.
7. Obtain the outputs of the following SQL queries based on the data given in tables PRODUCTS and
SUPPLIERS above.
ANSWER-
Output:
4
SOI1 (g1) s02
s03 (g2) 28000 1100
(g3) 550000 (g4)
PNAME SNAME
DIGITAL CAMERA 14X GETALL INC
PENDRIVE 16GB GETALL INC
Page 23 of 34
Question:15
CARDEN and CUSTOMER and answer (b) and (c) parts of this
1. To display the details of all the products in ascending order of product names (i.e., PNAME).
2. To display product name and price of all those products, whose price is in the range of 10000 and
15000 (both values inclusive).
3. To display the number of products, which are supplied by each supplier. i.e., the expected output
should be;
S01 2
S02 2
S03 1
4. To display the price, product name and quantity (i.e., qty) of those products which have quantity
more than 100.
5. To display the names of those suppliers, who are either from DELHI or from CHENNAI.
6. To display the name of the companies and the name of the products in descending order of company
names.
7. Obtain the outputs of the following SQL queries based on the data given in tables PRODUCTS and
SUPPLIERS above.
SELECT DISTINCT SUPCODE FROM PRODUCTS;
SELECT MAX (PRICE), MIN (PRICE) FROM PRODUCTS;
SELECT PRICE*QTY FROM PRODUCTS WHERE PID = 104;
SELECT PNAME, SNAME FROM PRODUCTS P, SUPPLIERS S WHERE P.SUPCODE =
S.SUPCODE AND QTY>100;
ANSWER-
Question:16
Consider the following tables CABHUB and CUSTOMER and answer (b) and (c) parts of this question :
Page 24 of 34
1. Give a suitable example of a table with sample data and illustrate Primary and Candidate Keys in it.
(ii) To display name of vehicle name and capacity of vehicles in ascending order of their sitting capacity.
(iii) To display the highest charges at which a vehicle can be hired from CABHUB.
(iv) To display the customer name and the corresponding name of the vehicle hired by them.
ANSWER-
Question:17
Consider the following tables EMPLOYEE and DEPARTMENT and answer (a) and (b) parts of this question.
Page 25 of 34
1. Write SQL commands for the following statements:
(i) To display all DepName along with the DepCde in descending order of DepCde. (ii) To display the average
age of Employees in DepCde as 103.
(iii) To display the name of DepHead of the Employee named “Sanjeev P”
(iv) To display the details of all employees who has joined before 2007 from EMPLOYEE table.
2. Give the output of the following SQL queries: (i) SELECT COUNT (DISTINCT DepCde) FROM
EMPLOYEE;
(ii) SELECT MAX(JoinDate), MIN (JointDate) FROM EMPLOYEE;
(iii) SELECT TName, DepHead FROM EMPLOYEE E, DEPARTMENT D WHERE E.DepCde =
D.DepCde;
(iv) SELECT COUNT (*) FROM EMPLOYEE WHERE Salary > 60000 AND Age > 30;
ANSWER-
Page 26 of 34
Question:18
Consider the following tables WORKER and PAYLEVEL and answer (a) and (b) parts of this question
(ii). To display NAME and DESIGN of those Workers, whose PLEVEL is either P001 or
(iii). To display the content of all the workers table, whose DOB is in between ’19-JAN- 1984′ and ’18-JAN-
1987′.
(iv). To add a new row with the following: 19, ‘DayaKishore’, ‘Operator’, ‘P003′, ’19- Sep-2008’, ‘ll-Jul-
1984’
(iii). SELECT Name,PAY FROM WORKER W,PAYLEVEL P WHERE W.LEVEL= P.PLEVEL AND
W.ECODE<13;
ANSWER-
(i). SELECT NAME FROM WORKER ORDER BY DOBDESC;
(ii). SELECT NAME, DESIGN FROM WORKER WHERE PLEVEL=”POOO1″ OR PLEVEL=”POO2″;
(iii). SELECT * FROM WORKER WHERE DOB BETWEEN ’19-JAN-1984 AND ’18-JAN-1987′;
(iv). INSERT INTO WORKER VALUES (19,”DAYAKISHORE”, “OPERATOR”, “P0003”,’19-Sep-
2008′,’11-Jul-1984′)
Page 27 of 34
Question:19
Consider the following tables EMPLOYEE and SALGRADE and answer (b) and (c) parts of this question:
2. To display NAME and DESIGN of those EMPLOYEES, whose SAL-GRADE is either S02 or S03.
3. TO display the content Of all the EMPLOYEES table, whose DOJ is in between ’09-Feb-2006′ and ’08-
Aug-2009′.
4. To add a new row with the following: 109, ‘HarishRoy’, ‘HEAD-IT’, ‘SOX, ’09- Sep-2007′, ’21-Apr-1983’
Page 28 of 34
ANSWER-
(b) 1. SELECT FROM EMPLOYEE ORDER BY DOJ DESC;
2. SELECT NAME, DESIGN FROM EMPLOYEE WHERE SGRADE – “S02” OR SGRADE = “SO3;
3. SELECT * FROM EMPLOYEE WHERE DOJ BETWEEN ’09-FEB-2006′ AND ’08- AUG -200%
4. INSERT INTO EMPLOYEE VALUES(109, “HARSH RAY”, “HEAD-IT.S02”, ’09-SEP- 2007′, ’21-
APR-1983′);
Question:20
Consider the following tables GAMES and PLAYER and answer (b) and (c) parts of this question :
Page 29 of 34
ANSWER
(a) An attribute or set of attributes which are used to identify a tuple uniquely is known as a primary key. If
a table has more than one such attributes which identify a tuple uniquely then all such attributes are known
as candidate keys.
(b)
(c)
1. 2
2. 19-Mar-2004 12-Dec-2003
3. Ravi Sahai Lawn Tennis
4. 101 108 103
Question:21
Consider the following tables ACTIVITY and COACH and answer (a) and (b) parts of this question
2. To display the sum of PrizeMoney for the Activities played in each of the Stadium separately.
3. To display the coach’s name and acodes in ascending order of Acode from the table Coach.
4. To display the content of the Activity table whose schedule date earlier than 01-01-2004 in ascending order
of Participants Num.
3. SELECT Name, Activity Name FROM ACTIVITY A, COACH C WHERE A.Acode=C.Acode AND
A.Parti- cipants Num=10;
ANSWER-
(a) An attribute or set of attributes which are used to identify a tuple uniquely is known as a primary key. If
a table has more than one such attribute which identifies a tuple uniquely, then all such attributes are known
as candidate keys.
(b)
(c)
1. 2
2. 19-Mar-2004 12-Dec-2003
3. Ravi Sahai Lawn Tennis
4. 101 108 103
Question:22
Following tables RESORT and OWNEDBY and answer (a) and (b) parts of this question:
2. To display the maximum and minimum rent for each type of resort from table RESORT.
3. To display the details of all resorts which are started after 31-DEC-05 from table RESORT.
4. Display the OWNER of all ‘5 STAR’ resorts from tables RESORT and OWNEDBY.
Page 31 of 34
(b) Give output for the following SQL queries:
(i). SELECT MIN(RENT) FROM RESORT Where PLACE = ‘KERALA’;
(ii). SELECT TYPE, START DATE FROM RESORT Where TYPE ‘2 STAR’ ORDERBY STARTDATE,
(iii). SELECT PLACE, OWNER FROM OWNEDBY Where PLACE LIKE “%A”;
(iv). SELECT RCODE, RENT FROM RESORT, OWNEDBY WHERE (RESORT PLACE= OWNEDBY.
PLACE AND TYPE = ‘3 STAR’);
(a)
1. SELECT RCODE, PLACE FROM RESORT mere TYPE = “5 STAR” ORDER BY PLACE;
3. SELECT FROM RESORT WHERE OSWAAL (BSE Question Bank. COMPUTER SCIENCE –
PYTHON, STARTDATE > ’31-DEC-05′;
4. SELECT OWNER FROM RESORT OWNEDBY B WHERE (A.TYPE START’ AND A.PLACE
B.PLACE);
ANSWER
1. SELECT RCODE, PLACE FROM RESORT WHERE TYPE = "5 STAR" ORDER BY PLACE;
2. SELECT MAX(RENT), MIN(RENT) FROM RESORT GROUP BY TYPE;
3. SELECT * FROM RESORT WHERE STARTDATE > '31-DEC-05';
4. SELECT OWNER FROM RESORT A, OWNEDBY B WHERE (A.TYPE = 'START' AND A.PLACE = B.PLACE);
Page 32 of 34
Question:23
Consider the following tables STORE and SUPPLIERS and answer (a) and (b) parts of this question:
2. To display ItemNo and Item name of those items from STORE table whose Rate is more than 15 Rupees.
3. To display the details of those items whose supplier code (Scode) is 22 or Quantity in Store (Qty) is more
than 110 from the table Store.
4. To display the minimum Rate of items for each supplier individually as per Scode from the table STORE.
4. SELECT Sname, MIN(Rate) FROM STORE, SUPPLIERS WHERE STORE. Scode = SUPPLIERS.Scode
GROUP BY Sname;
ANSWER
(a)
Page 33 of 34
(b)
1. 3
2. 880
3. Item Sname
Gel Pen Classic Premium Stationers
4. 24-Feb-10
Question:24
Following tables STOCK and DEALERS and answer (a) and (b) parts of this question:
ANSWER
(a)
(b)
1. 3
2. 4400
3.
Item Dname
Eraser Big Clear Deals
4. 01-Jan-09
*************************
Page 34 of 34