Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
5 views18 pages

Database N SQL Question Bank

The document contains a series of questions and tasks related to databases, including SQL queries, table structures, and concepts such as primary keys, foreign keys, and various SQL commands. It covers topics like data manipulation, data definition, and the differences between SQL commands. Additionally, it includes practical applications for creating and modifying database tables.

Uploaded by

alexalan2701
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views18 pages

Database N SQL Question Bank

The document contains a series of questions and tasks related to databases, including SQL queries, table structures, and concepts such as primary keys, foreign keys, and various SQL commands. It covers topics like data manipulation, data definition, and the differences between SQL commands. Additionally, it includes practical applications for creating and modifying database tables.

Uploaded by

alexalan2701
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

DATABASES

LEVEL 1

Q1. Consider the tables GSTCategory and Store_Items given below:


(a) Table: GSTCategory
CategoryID ​ Category_Name ​ GST
Cat1 ​ ​ Fresh Dairy ​ ​ 0
Cat10 ​ ​ DryFruit ​ ​ 5
Cat11 ​ ​ DryFruit ​ ​ 5
Cat12 ​ ​ Spice ​ ​ ​ 5
Cat13 ​ ​ Packaged Fat ​ ​ 12
Cat14 ​ ​ Packaged Oil ​ ​ 12
Cat15 ​ ​ DryFruit ​ ​ 12
Table Store_Items:
Item_Id ​ Item_Name​ Item_Brand ​ CategoryID ​ Price Unit
1010​ ​ Milk ​ ​ Verka ​ ​ Cat1 ​ ​ 20 litre
1012 ​ ​ Curd ​ ​ Amul ​ ​ Cat1 ​ ​ 50 kg
1003 ​ ​ Lassi​ ​ Vita​ ​ Cat1 ​ ​ 30 litre
1024 ​ ​ Chaach ​ Amul ​ ​ Cat1 ​ ​ 20 litre
1006 ​ ​ Eggs ​ ​ None ​ ​ Cat2 ​ ​ 48 dozen
1007 ​ ​ Gur ​ ​ None ​ ​ Cat3 ​ ​ 40 kg
1008 ​ ​ Honey ​ ​ None ​ ​ Cat3 ​ ​ 200 litre
1009 ​ ​ Salt ​ ​ None ​ ​ Cat4 ​ ​ 21 kg

(a) Identify the degree and cardinality of the GSTCategory table.


(b) Which field will be considered the foreign key if the tables GSTCategory and
Store_Items: are related in a database?
( c) Identify Primary key of both the Tables
(d) Identify foreign key in above table

Q2 . In the table Loan below (2)


(a) Identify the candidate key(s) from the table Loan.
(b) Which field will be considered as the foreign key if the tables Customers and Loan
are related in a database?
Table Loan
LoanID LoanDate ID EMI Years
1101 2021-03-01 5 20000 4
1102 2021-02-06 3 10000 2
1103 2021-04-12 4 10000 3
1104 2021-05-15 5 5000 3

Table Customers:
ID Name Age City Salary
1 Kashika 37 Jammu 80000.00
2 Anupriya 35 Shimla 75000.00
3 Vijayan 36 Hyderabad 65000.00
4 Krishnan 35 Chennai 55000.00
5 Harsh 37 Gandhinagar 85000.00
6 Raisa 32 Mumbai 45000.00

Q3. Write the SQL statement to add a field Country_Code(of type Integer) to the table Countries
with the following fields. Country_id, Country_name, Continent, Region_id

Q4. Which of the following is not a DML command?


DELETE FROM, DROP TABLE, CREATE TABLE, INSERT INTO

Q5. Which of the following is a DDL command?


DELETE FROM, DROP TABLE, CREATE TABLE, INSERT INTO

●​ Consider the table Travel given below:


TABLE: TRAVEL
CNO CNAME TRAVELDATE KM TAXI_CODE NOP
101 Randeep Singh 2018-11-07 200 T01 12
102 Sharad Bali 2018-12-21 120 T04 4
105 Sangeeta M 2019-04-25 450 T01 15
103 Manish Nagpal 2019-01-29 280 T02 5
107 Veronica Masih 2019-03-12 365 T04 2
104 Dinesh Hoon 2019-10-28 200 T05 4
106 Ramita Malik 2019-03-12 100 T01 20

(a) Identify the degree and cardinality of the table.


(b) Which field should be made the primary key? Justify your answer.
THEORY QUESTIONS
Differentiate between count() and count(*) functions in SQL with appropriate example.
Write the command to view all tables in a database
Which command is used to see the table structure
1.​ Which SQL logical operator allows the existence of multiple conditions in an SQL
statement's WHERE clause?
2.​ Which clause sorts the final data in the table?
3.​ Differentiate between DDL and DML Commands.
4.​ What is the difference between Cartesian Product and Join?
5.​ Differentiate between SQL and MYSQL
6.​ Differentiate between TABLE and Database
7.​ Give example of a tuple and Attribute
8.​ Give example of domain in RDBMS
9.​ Differentiate between Order By and Group By clause
10.​Differentiate between Equi Join and Natural Join
11.​Differentiate betweenHAVING and WHERE clause
12.​Which constraint specifies the values in the column must be unique?
13.​Which constraint specified with a column uniquely identifies each row in a table?
14. Which of the following commands will delete the table from MYSQL database?

●​ (a) DELETE TABLE


●​ (b) DROP TABLE
●​ (c) REMOVE TABLE
●​ (d) ALTER TABLE

15. Write is the difference between the Drop and Delete command of MYSQL. Write their syntax
also in SQL.

16. Which of the following types of table constraints will prevent the entry of duplicate rows? a)
Unique b) Distinct c) Primary Key d) NULL

OUTPUT QUESTIONS
Q1

Q2
Write the output of the queries (a) to (d) based on the table SCHOOLADMIN, given below: (2)

ADMNO NAME ​ STREAM​ ​ DOB​ PHONE ​ SECTION


101 ​ Srishti B ​ Business Admin ​ 2005-05-12 ​ 1234322934​ NULL
102 ​ Aman Pratap Sciences ​ ​ 2005-11-02 ​ 671343224 ​ B
103 ​ Shivam ​ Sciences ​ ​ 2006-01-23​ 511343224​ A
104 ​ Banita ​​ Business Admin​ 2005-10-12 ​ 132412934 ​ NULL
105 ​ Myra ​ ​ Fine Arts​ ​ 2005-07-01 ​ 124455931 ​ C
106 ​ Raghav ​ Humanities ​ ​ 2005-06-05 ​ 42155931​ D
107 ​ Medini​​ Fine Arts ​ ​ 2005-09-05​ 512155931 ​ C
108 ​ Udai Veer​ Sciences ​ ​ 2006-11-25 ​ 551343224​ B
109​ Gaurav ​ Legal Studies ​​ 2005-03-15 ​ 321155931 ​ NULL

a) SELECT Max(DOB) FROM SCHOOLADMIN;


b) SELECT Name FROM SCHOOLADMIN WHERE STREAM!="Business Admin" AND
SECTION IS NULL;
c) SELECT count(NAME) FROM SCHOOLADMIN WHERE SECTION IS NOT NULL;
d) SELECT count(NAME) FROM SCHOOLADMIN WHERE SECTION IS NOT NULL
AND STREAM="FINE ARTS";
Q3.
Table: Sportstars
Admn_No​ Name​​ DOB ​ ​ Sport ​ ​ Medals ​ Class
V1237​ ​ Vanshika​ 2005-02-19​ Tennis ​ 3​ ​ 11
S1432​ ​ Ruhaani ​ 2005-09-11 ​ Football​ 5​ ​ 12
SM1781 ​ Aalam​ ​ 2005-07-01​ Cricket​ 4​ ​ 11
V1935 ​​ Manan ​ 2005-04-07​ Football ​ 6​ ​ 11
S1276 ​​ Aarav ​ ​ 2005-10-06​ Tennis ​ 4​ ​ 12
SM1872 ​ Rehaan​ 2004-12-11 ​ Football​ 3​ ​ 12
a) SELECT COUNT(*) FROM Sportstars WHERE Medals>=4;
b) SELECT SUM(Medals) FROM Sportstars WHERE Class=12;
c) SELECT MAX(DOB) FROM Sportstars;
d) SELECT DOB FROM Sportstars WHERE Admn_No LIKE "SM%”

Q4. Write SQL queries for (i) to (iv) based on the following tables.
Table: TRAVEL.
NO ​ NAME​ ​ TDATE ​ KM ​ CODE ​NOP
101​ Janish​​ 2015-02-18 ​ 100 ​ 101​ 32
102 ​ Vedika​​ 2014-06-06 ​ 65 ​ 101 ​ 45
103 ​ Tarun ​ ​ 2012-10-09​ 32 ​ 104 ​ 42
104 ​ John ​ ​ 2015-10-30​ 55​ 105 ​ 40
105 ​ Ahmed ​ 2015-12-15​ 47​ 103 ​ 16
106 ​ Raveena ​ 2016-02-26 ​ 82 ​ 103​ 9
NO is Traveller Number
KM is Kilometers travelled
NOP is number of travellers in a vehicle
DATE is Travel Date
VCODE Vehicle Code
Table: VEHICLE
CODE​ VTYPE​ ​ PERKM
101​ VOLVO BUS ​​ 160
102 ​ AC BUS ​ ​ 150
104​ ORDINARY BUS ​ 80
103 ​ CAR ​ ​ ​ 25
105 ​ SUV ​ ​ ​ 40
a) To display NO, NAME,TDATE from the table Travel in the descending order of NO.
b) To display the NAME of all the travellers from the table TRAVEL who are
travelling by vehicle with code 101 or 102.
c) To display the NO and NAME of those travellers from the table TRAVEL who
travelled between 2015-12-31 and 2015-04-01.
d) Display maximum and minimum kilometres travelled by passengers.
e)Write a Query to display the details of all vehicle where vtype is like BUS

Consider the tables GSTCategory and Store_Items given below:


(a) Table: GSTCategory
CategoryID ​ Category_Name ​ GST%
Cat1 ​ ​ Fresh Dairy ​ ​ 0
Cat10 ​ ​ DryFruit ​ ​ 5
Cat11 ​ ​ DryFruit ​ ​ 5
Cat12 ​ ​ Spice ​ ​ ​ 5
Cat13 ​ ​ Packaged Fat ​ ​ 12
Cat14 ​ ​ Packaged Oil ​ ​ 12
Cat15 ​ ​ DryFruit ​ ​ 12
Table Store_Items:
Item_Id ​ Item_Name​ Brand ​CategoryID ​ PricePerUnit
1010​ ​ Milk ​ ​ Verka ​ ​ Cat1 ​ ​ 20 litre
1012 ​ ​ Curd ​ ​ Amul ​ ​ Cat1 ​ ​ 50 kg
1003 ​ ​ Lassi​ ​ Vita​ ​ Cat1 ​ ​ 30 litre
1024 ​ ​ Chaach ​ Amul ​ ​ Cat1 ​ ​ NULL
1006 ​ ​ Bread ​ ​ Britannia​ Cat2 ​ ​ 48 dozen
1007 ​ ​ Gur ​ ​ NULL​ ​ Cat3 ​ ​ 40 kg
1008 ​ ​ Honey ​ ​ Verka ​ ​ Cat3 ​ ​ 200 litre
1009 ​ ​ Butter​ ​ Amul ​ ​ Cat4 ​ ​ 21 kg

(a) Display the Item_name,PricePerUnit,Brand of MILK Eggs and Honey.


(b) Display Brand wiseTotal number of Products
( c) Reduce the price of Amul products by 5%
(d) Display Itemname,Brand, price, Category name through natural Join
(e) Display itemname of items where brand is NULL
(f) Display Itemname,Brand, price, Category name through Equi Join.
Q4
Q6
Q7.
Q8.

Q9 . Write the output of the queries (a) to (d) based on the table country, given below: (2)
Code Name Continent Surface area Population Life expectancy

AFG Afghanistan Asia 652090 22720000 46


AGO Angola Africa 1246700 12878000 39
AIA Anguilla North America 96 8000 76
ALB Albania Europe 28748 3401200 72
AND Andorra Europe 468 78000 84
ARE United Arab Emirates Asia 83600 2441000 74
ARG Argentina South America 2780400 37032000 75

ARM Armenia Asia 29800 3520000 66


ASM American Samoa Oceania 199 68000 75
ATA Antarctica Antarctica 13120000 0 Null
a) SELECT min(Population) FROM country;
b) SELECT max(SurfaceArea) FROM country;
c) SELECT avg(LifeExpectancy) FROM country Where Name Like "Ang%";
d) SELECT Code FROM country Where country.Name=country.Continent;

Q10.
Write SQL (i) to (v) based on following table :
a) To list the Employee Name who are not getting any commission(COMM).
b) Display Name of Employee whose name start with character ‘A’.
c) To count number of Jobs available in company i.e. in EMP table.
d) To display Employee no. and name of employees in decreasing order of Salary.
e) write a Query to display the no of records which have comm NULL

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

APPLICATION BASED
●​ Sahil wants to create a database HARDWARE which has a table RENTAL. He wants to
make sure that the city name is unique and no field is left empty in the table. Write the
SQL commands for the fields given below.

RENTAL_ID INT PRIMARY KEY


RENTAL_DATE DATETIME NOT NULL
INVENTORY_ID VARCHAR(6) NOT NULL
CUSTOMER_ID VARCHAR(6) NOT NULL
RETURN_DATE DATETIME NOT NULL
CITY VARCHAR(6) UNIQUE NOT NULL

●​ The database MavenAdmin already has a table DEPARTMENTS, write the SQL
statements to add a table named HR_jobs with the following structure.
Field Data Type Remarks
JOB_ID VARCHAR(10) Primary Key
JOB_TITLE VARCHAR(35) Not Null
DEPARTMENT_ID VARCHAR(10) Foreign Key for
DEPARTMENT_ID in
DEPARTMENTS Table

EXPERIENCE INT
MIN_SALARY DECIMAL(6,0)
MAX_SALARY DECIMAL(6,0)

14.​A table, SPORTSTARS has been created in a database with the following fields: (3)

Admn_No, Name, DOB, Sport, Medals, Class


Give the SQL command to rename the field Class to Grade in this table.

15.​
●​ The table FLIGHT_BAGGAGE shows data about the number of baggage , total weight in
kg for baggage and cost per extra kg for different classes. The PASSENGER table
shows the passengers with extra baggage weight. Using the information given below
write the SQL query for a)-d) (4)
TABLE FLIGHT_BAGGAGE
PAXCODE ​ PAX_CLASS​ BA ​ TKGA ​​ CPEKG
101​ ​ FIRST ​​ 3​ 90​ ​ 300
102 ​ ​ BUSINESS ​ 3 ​ 80 ​ ​ 300
103​ ​ ECOPLUS ​ 2 ​ 50​ ​ 400
104 ​ ​ ECONOMY​ 2 ​ 40 ​ ​ 500
105 ​ ​ SPECIAL ​ 4​ 100 ​ ​ 400

TABLE PASSENGER
NO ​ NAME ​​ TRAVELDATE​​ EKG ​ ​ PAXCODE​ NOB
11 ​ Ridhima Sen ​ 2019-12-13 ​ ​ 10 ​ ​ 101 ​ ​ 3
13 ​ Gary Sandhu ​ 2019-12-21 ​ ​ 15 ​ ​ 103​ ​ 3
15 ​ Ganesh V ​ 2019-12-23​ ​ 8​ ​ 102​ ​ 2
12 ​ Salim Malik ​ 2019-11-13 ​ ​ 15 ​ ​ 102 ​ ​ 3
17 ​ Geralene N ​ 2019-12-10​ ​ 11​ ​ 104​ ​ 2
14 ​ Venkatesh​ 2019-11-28​ ​ 8 ​ ​ 105​ ​ 4
16 ​ Saed Bajwa ​ 2019-11-06​ ​ 20​ ​ 101 ​ ​ 3

EKG- Extra KG

BA-Bags Allowed , CPEKG- Cost per extra KG, TKGA- Total Kilograms Allowed ,
NOB- Number of Bags

a) Write an SQL statement to display NO,NAME,TRAVELDATE from the table PASSENGER in


descending order of NO.
b) Write an SQL statement to display all the classes from table FLIGHT_BAGGAGE where
weight allowed is more than 75 Kg
c) Write an SQL statement to display the PAXCODE and their number from PASSENGER table,
grouped by the PAXCODE more than 1 in number.
d) Write an SQL statement to display the PAXCODE, NAME, PAX_CLASS from both tables
where Extra KG is more than 10
e) Change the CPEKG by increasing it by 10 % where EKG exceeds 10

Q6.
(ii) Write the Syntax for Equi- join and Natural Join

Golden Rainbow school has a Schooladmin Database with the following Tables. Using the
information given in the 2 tables write the SQL query for a)-d)
Table: Students
StudentID Name DOB City Sport Email
1211 Anvi 2005-10-06 Chandigarh Tennis [email protected]
1212 Ananya 2005-09-11 Panchkula TT [email protected]
1213 Bisman 2005-07-01 Mohali Cricket [email protected]
1214 Bani 2004-12-11 Chandigarh TT [email protected]
1215 Brie 2005-03-16 Chandigarh Squash [email protected]
1216 Diya 2006-07-11 Panchkula Tennis [email protected]
1217 Fiza 2005-07-21 Mohali Cricket [email protected]
1218 Gauri 2004-12-30 Chandigarh TT [email protected]

StudentID Unit1 Unit2 Term1 Term2


1211 23 24 37 39
1212 21 21 32 33
1213 25 24 38 39
1214 22 23 30 31
1215 21 24 37 39
1216 24 21 32 33
1217 23 24 38 39
1218 21 23 30 31

Write an SQL statement to display the StudentID, Name and Email of the students from
Chandigarh who play TT.
b) Write an SQL statement to display the Name and marks of Unit1 and Unit2 for those students
who scored above 44 marks after adding Unit1 and Unit2.
c) Write an SQL statement to display the Name and the total marks for the students from
Mohali.
d) Write an SQL statement to display the average of the total marks of the students.

Write the output of the queries (a) to (d) based on the table Sportstars, given below: (2)

●​ Write SQL commands for the queries (a)-(d) based on the two tables TAXITYPE and
TRAVEL (4)
●​
TABLE: TAXITYPE

TAXI_CODE ​ ​ TAXITYPE ​ ​ ​ PERKM


T01 ​ ​ ​ TEMPO TRAVELLER ​ ​ 40
T02 ​ ​ ​ AC INNOVA ​ ​ ​ 20
T03 ​ ​ ​ AC ERTIGA​ ​ ​ 15
T04 ​ ​ ​ AC HATCHBACK​ ​ 10
T05 ​ ​ ​ AC SEDAN ​ ​ ​ 10

TABLE: TRAVEL
CNO ​ CNAME​ ​ TRAVELDATE ​ KM​ TAXI_CODE​ ​ NOP
101​ Randeep Singh​​ 2018-11-07 ​ ​ 200 ​ T01 ​ ​ ​ 12
102​ Sharad Bali ​ ​ 2018-12-21​ ​ 120​ T04 ​ ​ 4
105 ​ Sangeeta M ​ ​ 2019-04-25 ​ ​ 450​ T01​ ​ ​ 15
103 ​ Manish Nagpal​ ​ 2019-01-29 ​ ​ 280 ​ T02 ​ ​ ​ 5
107​ Veronica Masih​​ 2019-03-12 ​ ​ 365 ​ T04 ​ ​ ​ 2
104 ​ Dinesh Hoon ​ ​ 2019-10-28​ ​ 290 ​ T05 ​ ​ ​ 4
106​ Ramita Malik​ ​ 2019-04-06 ​ ​ 100 T01 ​ ​ ​ 20
a) To display CNO, CNAME, TRAVELDATE from the table TRAVEL in descending order
of CNO.
b) To display the CNAME of all customers from the table TRAVEL who are travelling by
vehicle with code T01 or T02
c) To display the CNO and CNAME of those customers from the table TRAVEL who
travelled between '2019-01-06' and '2019-05-01'.
d) To display all the details from table TRAVEL for the customers, who have travel
distance more than 250 KM in ascending order of NOP (Number of Passengers)

●​ A table, Students has been created in a database with the following fields: (3)

StudentID, NAME, DOB, CITY, MARKS


Give the SQL command to change the datatype of CITY from Varchar(30) to Char(30).
(ii) Which of the following is a DML command?
SELECT...FROM, CREATE TABLE, INSERT, UPDATE

●​ Maniklal Fabrics is creating a database called Stores with


a table called City_Store. Write the SQL commands for
doing the same. (3)

Store_id INT Primary Key


Manager Varchar(20)
Address Varchar(30)
City Varchar(20)
City_Code Char(5)
Last_update Date

●​ A database called ecompany has two tables COMPANY and CUSTOMER with the
following records.
Write SQL commands for the queries (a)-(d) based on the two tables COMPANY and
CUSTOMER
COMPANY

PID ​ NAME ​​ ​ CITY ​ ​ PRODUCTNAME


2101 ​ APPLE ​ ​ DELHI​​ ​ WATCH
2102 SAMSUNG ​ ​ BANGALORE ​ MOBILE
2103 PANASONIC​ ​ DELHI ​ ​ TV
2104 SONY​ ​ ​ MUMBAI ​ ​ MOBILE
2105 LENOVO ​ ​ INDORE​ ​ TABLET
2106 DELL​ ​ ​ MUMBAI​ ​ LAPTOP

CUSTOMER

CUSTID ​ NAME​ ​ ​ UNITPRICE​ QTY​ ​ PID


101​ ​ REENA SONI​​ 60,000 ​ 10 ​ ​ 2102
102 ​ ​ MICHAEL PAUL​ 50,000 ​ 20 ​ ​ 2106
103​ ​ MEETALI SINGH​ 70,000 ​ 15​ ​ 2101
104​ ​ PARUL SOHAL ​ 55,000 ​ 3​ ​ 2103
105​ ​ RAJESH DESWAL​ 45,000 ​ 7​ ​ 2104

a)​ Identify degree and cardinality of both Tables


b)​ Identify the primary key of both tables
c)​ Identify foreign key
d)​ What is primary key
e)​ Differentiate between Candidate Key and Alternate key
f)​ Identify Tuple, Attribute from above
g)​ Explain , what is domain?

a) Write an SQL statement to display the name of the companies in reverse alphabetical order.
b) To add one more column to the table customer called TOTAL_PRICE which can have up to
two
decimal places.
c) Write an SQL statement to count the products city wise.
d) Write an SQL statement to display the customer name, product of the unit price and quantity,
product name where the name of the city is Mumbai.
PYTHON MYSQL
Q4. Which method of the Cursor Class is used to execute an SQL statement? What does the
cursor( ) do?

A resultset is extracted from the Customer table using the cursor object (that has been already
created) by giving the following statement.
Rec_Data=cursor.fetchall()
(a) How many records of the table will be returned by fetchall() method?
(b) What will be the datatype of Rec_Data object after the given command is executed?

Q6. (i) Which clause aggregates the base data? (2)

Which clause filters the aggregated data?


(ii) What is a natural join? (2)

(SECTION B)
Each question carries 3 marks

Q10. (SECTION C)
Each question carries 4 marks

What is the datatype of the result set extracted from the database using the cursor object when
we use
the following: (2)
a) fetchone( ) method b) fetchall( ) method

You might also like