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

Unit 3- Database Management

This document outlines the CBSE syllabus for Database Management, covering fundamental concepts such as database structures, relational data models, and SQL. It includes detailed explanations of SQL commands, data types, constraints, and how to interface SQL with Python for database operations. Additionally, it features multiple-choice questions and fill-in-the-blank exercises to reinforce understanding of the material.

Uploaded by

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

Unit 3- Database Management

This document outlines the CBSE syllabus for Database Management, covering fundamental concepts such as database structures, relational data models, and SQL. It includes detailed explanations of SQL commands, data types, constraints, and how to interface SQL with Python for database operations. Additionally, it features multiple-choice questions and fill-in-the-blank exercises to reinforce understanding of the material.

Uploaded by

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

UNIT 3: DATABASE MANAGEMENT

CBSE SYLLABUS
1. Database Concepts

 Introduction to database concepts and its need

2. Relational Data Model

 Relation, Attribute, Tuple, Domain, Degree, Cardinality


 Keys: Candidate Key, Primary Key, Alternate Key, Foreign Key

3. Structured Query Language (SQL)

 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

4. Interface of Python with SQL Database

 Connecting SQL with Python


 Performing Queries Using Cursor:
o INSERT, UPDATE, DELETE Queries
 Displaying Data Using:
o connect(), cursor(), execute(), commit(), fetchone(), fetchall(), rowcount
 Creating Database Connectivity Applications
 Use of %s Format Specifier or format() to Perform Queries

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.

2. Relational Data Model


 Relation: A table in a database.
 Attribute: A column in a table.
 Tuple: A row in a table.
 Domain: Allowed values for an attribute.
 Degree: Number of attributes (columns).
 Cardinality: Number of tuples (rows).
Types of Keys
Key Description Example
Candidate Key Unique identifiers in a table Roll No, Admission No
Primary Key Chosen unique key for a table Roll No
Alternate Key Other candidate keys not chosen as Primary Key Admission No
Foreign Key References primary key of another table Student_ID in Fee Table

3. Structured Query Language (SQL)


Data Types in SQL
Data Type Description Example
char(n) Fixed-length string char(5) → 'Hello'
varchar(n) Variable-length string varchar(10) → 'Database'
int Integer values int → 1001
float Decimal values float → 99.99
date Stores date values '2024-02-20'
Constraints in SQL
Constraint Description

NOT NULL Ensures a column cannot have NULL values

UNIQUE Ensures unique values in a column

PRIMARY KEY Ensures unique and NOT NULL values

Basic SQL Commands


Database Creation and Management
CREATE DATABASE SchoolDB;
USE SchoolDB;
SHOW DATABASES;
DROP DATABASE SchoolDB;

Table Creation and Modification


CREATE TABLE Student (
RollNo INT PRIMARY KEY,
Name VARCHAR(50) NOT NULL,
Age INT,
Class VARCHAR(10)
);

DESCRIBE Student;

ALTER TABLE Student ADD Address VARCHAR(100);

Page 2 of 34
ALTER TABLE Student DROP COLUMN Address;

ALTER TABLE Student ADD PRIMARY KEY (RollNo);

DROP TABLE Student;

Data Manipulation (DML)


INSERT INTO Student (RollNo, Name, Age, Class) VALUES (101, 'Amit', 16, '10A');

DELETE FROM Student WHERE RollNo = 101;

UPDATE Student SET Age = 17 WHERE RollNo = 101;


SELECT * FROM Student;

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';

Other SQL Clauses


SELECT DISTINCT Class FROM Student;
SELECT * FROM Student WHERE Age BETWEEN 14 AND 18;
SELECT * FROM Student WHERE Class IN ('10A', '10B');
SELECT * FROM Student ORDER BY Name ASC;
SELECT * FROM Student WHERE Name LIKE 'A%'; -- Names starting with A

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;

GROUP BY & HAVING


SELECT Class, COUNT(*) FROM Student GROUP BY Class;
SELECT Class, COUNT(*) FROM Student GROUP BY Class HAVING COUNT(*) > 10;

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()

Fetching Data from Database


cursor.execute("SELECT * FROM Student")
records = cursor.fetchall() # Fetch all records
for row in records:
print(row)

Using Format Specifiers


roll_no = 103
name = "Suman"
age = 16
class_name = "10B"

cursor.execute("INSERT INTO Student (RollNo, Name, Age, Class) VALUES (%s, %s, %s, %s)",
(roll_no, name, age, class_name))
conn.commit()

Multiple Choice Questions


Question 1
A relational database consists of a collection of
1. Tables
2. Fields
3. Records
4. Keys
Answer: 1. Tables
Question 2
A relational database consists of a collection of
1. Tuples
2. Attributes
3. Relations
4. Keys
Answer: 3. Relations
Question 3
A(n) ............... in a table represents a logical relationship among a set of values.
Page 4 of 34
1. Attribute
2. Key
3. Tuple
4. Entry
Answer: 3. Tuple
Question 4
The term ............... is used to refer to a record in a table.
1. Attribute
2. Tuple
3. Field
4. Instance
Answer: 2. Tuple
Question 5
The term ............... is used to refer to a field in a table.
1. Attribute
2. Tuple
3. Row
4. Instance
Answer: 1. Attribute
Question 6
A ............... is a property of the entire relation, which ensures through its value that each tuple is unique in a
relation.
1. Rows
2. Key
3. Attribute
4. Fields
Answer: 2. Key
Question 7
Which of the following attributes cannot be considered as a choice for primary key?
1. Id
2. License number
3. Dept_id
4. Street
Answer: 4. Street
Question 8
An attribute in a relation is a foreign key if it is the ............... key in any other relation.
1. Candidate
2. Primary
3. Super
4. Sub
Answer: 2. Primary
Question 9
Consider the table with structure as:
Student(ID, name, dept name, tot_cred)
In the above table, which attribute will form the primary key?
1. Name
2. Dept
3. Total_credits
4. ID
Answer: 4. ID
Question 10
Which of the following is not a legal sub-language of SQL?
1. DDL
2. QAL

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;

(v) SELECT MIN(ISSUEDATE) FROM MEMBER;


SELECT MIN(ISSUEDATE) FROM MEMBER;

Question:3
Give a suitable example of a table with sample data and illustrate Primary and Candidate keys.

Answer: Illustration of Primary and Candidate Keys


A Primary Key is a unique identifier for a record in a table. It must contain unique values, and it cannot
contain NULL values.
A Candidate Key is a set of attributes that can uniquely identify a row in a table. Out of multiple candidate
keys, one is chosen as the primary key.

Example Table: STUDENT


RollNo StudentName Email Phone Class

101 Aman Verma [email protected] 9876543210 10

102 Nisha Roy [email protected] 8765432109 10

103 Rahul Mehta [email protected] 7654321098 11

104 Simran Kaur [email protected] 6543210987 11

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

SQL Queries with Answers


(i) To display CNO, CNAME, TRAVELDATE from the table TRAVEL in descending order of CNO.
SELECT CNO, CNAME, TRAVELDATE
FROM TRAVEL
ORDER BY CNO DESC;
✅ Output:
CNO CNAME TRAVELDATE
107 John Malina 2015-02-10
106 Ramesh Jaya 2016-04-06
105 Hitesh Jain 2016-04-23

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

(vi) To display all distinct VCODE from TRAVEL.


SELECT DISTINCT VCODE FROM TRAVEL;
✅ Output:
Page 14 of 34
VCODE
V01
V02
V03
V04
V05

(vii) To display VCODE, CNAME, and VEHICLETYPE from TRAVEL and VEHICLE tables for those who
traveled less than 90 KM.

SELECT A.VCODE, CNAME, VEHICLETYPE


FROM TRAVEL A, VEHICLE B
WHERE A.VCODE = B.VCODE
AND KM < 90;

✅ Output:

VCODE CNAME VEHICLETYPE

V04 John Malina CAR

V05 Sahanubhuti SUN

(viii) To display CNAME and total fare (KM * PERKM) for all customers except those traveling by 'V05'.

SELECT CNAME, KM * PERKM AS TotalFare


FROM TRAVEL A, VEHICLE B
WHERE A.VCODE = B.VCODE
AND A.VCODE != 'V05';

✅ Output:

CNAME TotalFare

K. Niwal 30000

Fredrick Sym 9600

Hitesh Jain 56250

Ravi Anish 10000

John Malina 1170

Ramesh Jaya 15000

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;

2. SELECT max (EXPERIENCE) FROM SCHOOL;

3. SELECT TEACHER FROM SCHOOL WHERE EXPERIENCE >12 ORDER BY TEACHER;

4. SELECT COUNT (*), GENDER FROM ADMIN GROUP BY GENDER;

ANSWER-

(i) VICE PRINCIPAL 01


(ii) 16
(iii) UMESH YASH RAJ
(iv) 5 MALE 2 FEMALE

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

Note: PERKS is Freight Charages per


Note: NO is Driver Number
KM is Kilometer travelled
NOP is number of travellers travelled in vehicle
TDATE is Trip Date
1. To display NO, NAME, TDATE from the table TRIP in descending order of NO.
2. To display the NAME of the drivers from the table TRIP who are traveling by transport vehicle with code
101 or 103.
3. To display the NO and NAME of those drivers from the table TRIP who travelled between ‘2015-02-10’
and ‘2015-04-01’.
4. To display all the details from table TRIP in which the distance travelled is more than 100 KM in ascending
order of NOP
5. SELECT COUNT (*), TCODE From TRIP GROUP BY TCODE HAVNING COUnT (*) > 1;
6. SELECT DISTINCT TCODE from TRIP;
7. SELECT A.TCODE, NAME, TTYPE FROM TRIP A, TRANSPORT B WHERE A. TCODE = B. TCODE
AND KM < 90;
8. SELECT NAME, KM *PERKM FROM TRIP A, TRANSPORT B WHERE A. TCODE = B. TCODE
AND A. TCODE = 105′;

ANSWER-

1. SELECT NO, NAME, TDATE FROM TRIP ORDER BY NO;


2. SELECT NAME FROM TRIP WHERE TCODE = 101 OR TCODE = 103;
3. SELECT NO AND NAME FROM TRIP WHERE ‘2015-02-10’ < TDATE < ‘2015-04-01’;
4. SELECT NO, NAME, TDATE, KM, TCODE FROM TRIP WHERE KM >100 ORDER BY NOP;
5. TO DISPLAY THE MORE THAN ONE COUNT OF TCODE FROM THE TABLE TRIP
6. TO DISPALY SEPERATE TCODE OF TABLE TRIP
7. TO DISPAY THE NAME AND CODE OF THOSE TRANS PORTERS, WHO HAVE
TRAVELLED MORE THAN 90 KMS.
8. TO DISPLAY THE NAME AND EXPENDITARE OF A TRANSPORTER WHO HAVE TCODE
AS 105.

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-

Difference between DELETE and DROP Table Commands:

Feature DELETE Command DROP Command


Used to remove the entire table from the
Purpose Used to remove specific records from a table
database
Deletes rows but retains table structure and Deletes the table along with its structure and
Effect
schema schema
Can be rolled back using ROLLBACK if within a Cannot be rolled back; the table is permanently
Rollback
transaction removed
Usage DELETE FROM table_name WHERE condition; DROP TABLE table_name;
Triggers Triggers can be activated No triggers are activated

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.

2. To display the name of the companies in reverse alphabetical order.

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;

6. SELECT MIN(PRICE), MAX(PRICE) FROM CUSTOMER WHERE QTY>10;

7. SELECT AVG(QTY) FROM CUSTOMER WHERE NAME LIKE “%r%;

8. SELECT PRODUCTNAME,CITY, PRICE FROM COMPANY, CUSTOMER WHERE COMPANY.


CID=CUSTOMER.CID AND PRODUCTNAME=”MOBILE”;
ANSWER-

1. SELECT NAME FROM COMPANY WHERE COMPANY.CID=CUSTOMER. CID AND PRICE


< 30000;
2. SELECT NAME FROM COMPANY ORDER BY NAME DESC;
3. UPDATE CUSTOMER SET PRICE = PRICE + 1000 WHERE NAME LIKE ‘S%’;
4. ALTER TABLE CUSTOMER ADD TOTALPRICE DECIMAL(10,2);
5. 3 DELHI 2 MUMBAI 1 MADRAS
6. 50000,70000
7. 11
8. MOBILE MUMBAI 70000 MOBILE MUMBAI 25000

Question:11
Consider the following tables SCHOOL and ADMIN and answer

Write SQL statements for the following:

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.

3. To display DESIGNATION without dupli¬cate entries from the table ADMIN.

4. To display TEACHERNAME, CODE and corresponding DESIGNATION from tables SCHOOL and
ADMIN of Male teachers.

Page 19 of 34
ANSWER-

1. SELECT TEACHERNAME, PERIODS FROM SCHOOL WHERE PERIODS>25:


2. SELECT * FROM SCHOOL;
3. SELECT DISTINCT DESIGNATION FROM ADMIN;
4. SELECT TEACHERNAME.CODE DESIGNATION FROM SCHOOL.CODE = ADMIN.CODE
WHERE GENDER = MALE;

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.

3. TO DISPLAY TOTAL QUANTITY IN STORE OF UNISEX TYPE WATCHES.

4. TO DISPLAY WATCH NAME AND THEIR QUANTITY SOLD IN FIRST QUARTER;

5. SELECT MAX (PRICE), MIN(QTY_STORE) FROM WATCHES;

6. SELECT QUARTER, SUM(QTY SOLD) FROM SALE GROUP BY QUARTER;

7. SELECT WATCH_NAME, PRICE, TYPE FROM WATCHES W, SALE S WHERE


W. WATCH1D!=S.WATCHID;

8. SELECT WATCH_NAME, QTYSTORE, SUM (QTY_SOLD), QTY_STORESUM (QTYSOLD)


“STOCK” FROM WATCHES W, SALE S WHERE W. WATCHID = S.WATCHID GROUP BY
S.WATCHID;

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.

(a) Write the SQL queries:


1. To display Name and Price of all the Accessories in ascending order of their Price.
2. To display Id and SName of all Shop located in Nehru Place.
3. To display Minimum and Maximum Price of each Name of Accessories.
4. To display Name, Price of all Accessories and their respective SName where they are available.

(b) Write the output of the following SQL


1. SELECT DISTINCT NAME FROM ACCESSORIES WHERE PRICE> =5000;
2. SELECT AREA, COUNT(*) FROM SHOPPE GROUP BY AREA;
Page 21 of 34
3. SELECT COUNT (DISTINCT AREA) FROM SHOPPE;
4. SELECT NAME, PRICE*0.05 DISCOUNT FROM ACCESSORIES WHERE SNO IN (‘S02‘,S03‘);

ANSWER-

(a)

1. SELECT Name, Price FROM ACCESSORIES ORDER BY Price ASC;


2. SELECT ID, SName FROM SHOP WHERE Area=”Nehru Place”;
3. SELECT Name, MAX(Price), MIN(Price) FROM ACCESSORIES GROUP BY Name;
4. SELECT Name, Price, SName FROM ACCESSORIES, SHOP WHERE SHOP.ID =
ACCESSORIES.ID;

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-

1. SELECT * FROM PRODUCTS ORDER BY PNAME ASC;


2. SELECT PNAME, PRICE FROM PRODUCTS WHERE ((PRICE >= 10000) AND (PRICE <=
15000));
3. SELECT SUPCODE, COUNT(PID) FROM PRODUCTS GROUP BY SUPCODE;
4. SELECT PRICE, PNAME, QTY FROM PRODUCTS WHERE (QTY > 100);
5. SELECT SNAME FROM SUPPLIERS WHERE ((CITY = "DELHI") OR (CITY = "CHENNAI"));
6. SELECT COMPANY, PNAME FROM PRODUCTS ORDER BY COMPANY DESC;

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-

1. SELECT * FROM PRODUCTS ORDER BY PNAME ASC;


2. SELECT PNAME, PRICE FROM PRODUCTS WHERE ((PRICE => 10000) AND (PRICE = <
15000));
3. SELECT SUPCODE, COUNT (PID) FROM PRODUCTS GROUP BY SUPCODE;
4. SELECT PRICE, PNAME, QTY FROM PRODUCTS WHERE (QTY > 100);
5. SELECT SNAME FROM SUPPLIERS WHERE ((CITY = “DELHI”) OR (CITY = “CHENNAI”));
6. SELECT COMPANY, PNAME FROM PRO-DUCTS ORDER BY COMPANY DESC;
7. SOI1 (gl) S02 S03 (g2) 28000 1100 (g3) 550000 (g4) PNAME SNAME Vi DIGITAL CAMERA 14
X GETALL INC PENDRIVE16 GB GETALL INC

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.

2. Write SQL commands for the following statements:


(i) To display the names of all the white coloured vehicles.

(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.

3. Give the output of the following SQL queries :


(i) SELECT COUNT(DISTINCT Make) FROM CABHUB;

(ii) SELECT MAX(Charges), MIN(Charges)


FROM CABHUB;

(iv) SELECT COUNT (*) Make FROM CABHUB;

(v) SELECT Vehicle FROM CABHUB WHERE Capacity=4;

ANSWER-

1. Primary key of CABHUB = Vcode


Alternate key of CABHUB = Vehicle Name.
Primary key of CUSTOMER = Ccode
Alternate Key of CUSTOMER = Cname.
2. (i) SELECT VehicleName FROM CABHUB WHERE Colour = “WHITE”;
(ii) SELECT VehicleName, Capacity FROM CABHUB ORDER BY Capacity ASC;
(iii) SELECT MAX(Charges) FROM CABHUB;
(iv) SELECT Cname, VehicleName FROM CABHUB, CUSTOMER WHERE CUSTOMER.Vcode
= CABHUB.Vcode;
3. (i) 4
(ii) MAX(Charges) MIN(Charges) 35 12
(iii) 5
(iv) SX4
(v) C Class

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-

(i) SELECT DEPNAME, DEPARTMENT. DepCde FROM EMPLOYEE, DEPARTMENT WHERE


EMPLOYEE. DepCDE= DEPARTMENT. DepCde Order by DepCde DESC;
(ii). Select AVG (Age) from EMPLOYEE WHERE DepCde=”103″;
(iii). SELECT DeptHead FROM DEPART MENT WHERE Employee. TName= “Sanjeev P” AND
EMPLOYEE. DepCde = DEPARTMENT. DepCde;
(iv). SELECT * from EMPLOYEE WHERE joinDate<’01-JAN-2007′;

Page 26 of 34
Question:18
Consider the following tables WORKER and PAYLEVEL and answer (a) and (b) parts of this question

(a) Write SQL commands for the following statements:

(i). To display the name of all Workers in descending order of DOB.

(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’

(b) Give the output of the following SQL queries :


(i). SELECT COUNT (PLEVEL), PLEVEL FROM WORKER GROUP BY PLEVEL;

(ii). SELECT MAX(DOB), MIN(DOJ) FROM WORKER;

(iii). SELECT Name,PAY FROM WORKER W,PAYLEVEL P WHERE W.LEVEL= P.PLEVEL AND
W.ECODE<13;

(iv). SELECT PLEVEL, PAYLEVEL WHERE PLEVEL=”POO3″;

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:

(b) Write SQL commands for the following statements :


1. To display the details of all EMPLOYEES in descending order of DOJ.

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’

(c) Give the output of the following SQL queries :


1. SELECT COUNT(SGRADE), SGRADE FROM EMPLOYEE GROUP BY SGRADE;

2. SELECT MIN(DOB), MAX(DOJ) FROM EMPLOYEE;

3. SELECT NAME, SALARY FROM EMPLOYEE E, SAL-GRADE S WHERE E.SGRADE= S.SGRADE


AND E.ECODE<103′;

4. SELECT SGRADE, SALARY +HRA FROM SALGRADE WHERE SGRADE =SGRADE=’S02;

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 :

(a) What do you understand by primary key and candidate keys?

(b) Write SQL commands for the following statements:

1. To display the name of all GAMES with their GCodes.


2. To display details of those GAMES which are having PrizeMoney more than 7000.
3. To display the content of the GAMES table in ascending order of Schedule Date.
4. To display the sum of PrizeMoney for each type of GAMES.

(c) Give the output of the following SQL queries:

1. SELECT COUNT(DISTINCT Number) FROM GAMES;


2. SELECT MAX(ScheduleDate), MIN(ScheduleDate) FROM GAMES;
3. SELECT Name, GameName FROM GAMES G, PLAYER P WHERE (G.Gcode = P.Gcode AND G.PrizeMoney >
10000);
4. SELECT DISTINCT Gcode FROM PLAYER;

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)

1. SELECT GameName, GCode FROM GAMES;


2. SELECT * FROM Games WHERE PrizeMoney > 7000;
3. SELECT * FROM Games ORDER BY ScheduleDate;
4. SELECT SUM(PrizeMoney) FROM Games GROUP BY Type;

(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

(a) Write SQL commands for the following statements:


1. To display the names of all activities with their A codes in descending order.

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.

(b) Give the output of the following SQL queries:


1. SELECT COUNT (DISTINCT Participants Num) FROM ACTIVITY;

2. SELECT MAX (Schedule Date), Min (Schedule Date) FROM ACTIVITY;

3. SELECT Name, Activity Name FROM ACTIVITY A, COACH C WHERE A.Acode=C.Acode AND
A.Parti- cipants Num=10;

4. SELECT DISTINCT Acode FROM COACH;


Page 30 of 34
(a) 1. SELECT Acodes, ActivityName FROM ACTIVITY ORDER BY ACode DESC;

2. SELECT SUM(PrizeMoney) FROM ACTIVITY GROUP BY Stadium;

3. SELECT Name, Acode FROM COACH ORDER BY Acode;

4. SELECT * FROM ACTIVITY WHERE SchduleDate < ’01-Jan-2004′ ORDER BY ParticipantsNum;

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)

1. SELECT GameName, GCode FROM GAMES;


2. SELECT * FROM Games WHERE PrizeMoney > 7000;
3. SELECT * FROM Games ORDER BY ScheduleDate;
4. SELECT SUM(PrizeMoney) FROM Games GROUP BY Type;

(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:

(a)Write SQL commands for the following statements:


1. To display the RCODE and PLACE of all ‘5 STAR’ resorts in the alphabetical order of the place from table
RESORT.

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;

2. Select MAX (RENT), MIN (RENT) FROM RESORT GROUP BY TYPE;

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:

(a) Write SQL commands for the following statements:


1. To display details of all the items in the STORE table in ascending order of BestBuy.

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.

(b) Give the output of the following SQL queries:

1. SELECT COUNT(DISTINCT Scode) FROM STORE;

2. SELECT Rate* Qty FROM STORE WHERE ItemNo=2004;

3. SELECT Item, Sname FROM STORE S, Suppliers P

4. WHERE S.Scode=PScode AND ItemNo=2006;

5. SELECT MAX(LastBuy) FROM STORE;


(a) 1. SELECT * FROM STORE ORDER BY LastBuy ASC;

2. SELECT ItemNo, Item FROM STORE WHERE Rate > 15;

3. SELECT * FROM STORE WHERE (Scode = ’22’ OR Qty >’110′);

4. SELECT Sname, MIN(Rate) FROM STORE, SUPPLIERS WHERE STORE. Scode = SUPPLIERS.Scode
GROUP BY Sname;

ANSWER

(a)

1. SELECT * FROM STORE ORDER BY LastBuy ASC;


2. SELECT ItemNo, Item FROM STORE WHERE Rate > 15;
3. SELECT * FROM STORE WHERE (Scode = '22' OR Qty > '110');
4. SELECT Sname, MIN(Rate) FROM STORE, SUPPLIERS WHERE STORE.Scode = SUPPLIERS.Scode GROUP BY
Sname;

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:

(a) Write SQL commands for the following statements:


1. To display the details of all Items in the STOCK table in ascending order of StockDate.
2. To display ItemNo and Item name of those items from STOCK table whose UnitPrice is more than Rupees
10.
3. To display the details of those items whose dealer code (Dcode) is 102 or quantity in STOCK (Qty) is more
than 100 from the table Stock.
4. To display maximum UnitPrice of items for each dealer individually as per Dcode from the table STOCK.
(b) Give the output of the following SQL queries:
1. SELECT COUNT(DISTINCT Dcode) FROM STOCK;
2. SELECT Qty* UnitPrice FROM STOCK WHERE ItemNo=5006;
3. SELECT Item, Dname FROM STOCK S, Dealers D WHERE S.Dcode=D.Dcode AND ItemNo = 5004;
4. SELECT MIN (StockDate) FROM STOCK;

ANSWER

(a)

1. SELECT * FROM STOCK ORDER BY StockDate;


2. SELECT ItemNo, Item FROM STOCK WHERE UnitPrice > 10;
3. SELECT * FROM DEALERS, STOCK WHERE (DEALERS.Dcode = "102" OR STOCK.Qty > 100 AND
DEALERS.DCODE = STOCK.DCODE);

4. SELECT MAX(UnitPrice) FROM DEALERS, STOCK ORDER BY STOCK.Dcode WHERE DEALERS.Dcode =


STOCK.Dcode;

(b)
1. 3
2. 4400
3.
Item Dname
Eraser Big Clear Deals
4. 01-Jan-09

*************************

Page 34 of 34

You might also like