You are on page 1of 29

My SQL questions

MovieID MovieName Category ReleaseDate ProductionCost BusinessCos


t
001 Hindi_Movie Musical 2018/04/23 124500 130000
002 Tamil_Movie Action 2016/05/17 112000 118000
003 English_Movie Horror 2017/08/06 245000 360000
004 Bengali_Movie Adventure 2017/01/04 72000 100000
005 Telugu_Movie Action - 100000 -
006 Punjabi_Movie Comedy - 30500 -
1- Consider the following table MOVIE and write the query for
the following:

a) Retrieve MOVIE info without mentioning their column names.


b) List business done by the MOVIE showing their ID, Names and Business Cost.
c) List the different categories of the MOVIEs.
d) Find the net profit of each MOVIE showing its ID, Name and Net Profit (Net Profit =
Business Cost – Production Cost)
e) List all the MOVIEs with Production Cost greater than 80,000 and less than 1,25,000
showing its ID, Name and production Cost.
f) List all the MOVIE which fall in the category of Comedy or Action.
g) List all the MOVIEs which have not been released yet.
h) Display the name of lastest movie.

ANSWERS

a) SELECT * FROM MOVIE ;


b) SELECT MovieId , MovieName ,BusinessCost FROM MOVIE;
c) SELECT Distinct(Category) FROM MOVIE;
d) SELECT MovieID, MovieName ,BussinessCost-ProductionCost as ‘Net_Profit’ FROM
MOVIE;
e) SELECT MovieID, MovieName, ProductionCost FROM MOVIE;
f) SELECT * FROM MOVIE where Category = ‘Comedy’ or category = ‘Action’;
g) SELECT * FROM MOVIE where ReleaseDate is NULL;
h) SELECT MovieName from Movie where ReleaseDate= max(ReleaseDate) ;
2- Consider the following table and their structures , which are part of
common database.
Customer (CustomerID , CompanyName)
Software (SoftwareID, SoftwareName, OperatingSystem, Description)
License (LicenseID, CustomerId, SoftwareId, DateOfPurchase, Licensetype,
Cost, ExpiryDate)

(a) Identify the keys(Primary Key and Foreign Key)from each table.
(b) Write a query to display all software for the operating system ‘Linux’ from
the table Software.
(c) Write a Query to list all the license where cost is more than Rs.35000.
Group the output by CustomerId, and in ascending order of cost.
(d) For the License purchased after January 1, 2021 , the cost has been
increased by 10% . Write a query to achieve this.
(e) Write a Query to add a column category in the table customer so that it
should be able to store one of these categories (‘Domestic’, ‘International’,
‘government’, ‘Military’)

Answers
(a)
Table Primary Key Foreign Key
CUSTOMER CustomerId None
SOFTWARE SoftwareId None
LICENSE LicenseId CustomerId(with table Customer)
SoftwareId(with table Software)

(b) Select * from Software where OperatingSystem = ‘Linux’;


(c) Select * from License where cost>35000 Group by customerID Order by
cost ;
(d) Update License Set cost = cost+(cost*0.10) where DateofPurchase>=
‘2021-01-01’ ;
(e) Alter Table Customer Add (Category Varchar(20)) ;

3- A Department store MyStore is considering to maintain their


inventory using SQL to store the data.
ItemNO ItemName Scode Quantity
2005 Sharpener Classic 23 60
2003 Ball Pen 0.25 22 50
2002 Gel Pen Premium 21 150
2006 Gel Pen Classic 21 250
2001 Eraser Small 22 220
2004 Eraser Big 22 110
2009 Ball Pen 0.5 21 180

(a) Identify the attribute best attribute best suitable for a Primary Key.
(b) Write the cardinality and Degree of the table.
(c) Insert the following data into attributes ItemNO, ItemName and Scode respectively in
the given table
ItemNo=2010, ItemName= ‘Note book’ , and Scode=25
(d) Which command can be used to remove the table Store from the Database
Permanently?
(e) Write a Query to display the structure of the table STORE.

ANSWERS

(a) ItemNo
(b) Degree=4, Cardinality=7.
(c) Insert into Store (ItemNo, ItemName,Scode) Values(2010, “Note Book”, 25);
(d) Drop table Store;
(e) Describe Store;

4- Write SQL commands for the following on the basis of table


HOSPITAL :

OUR OWN HIGH SCHOOL, AL WARQA’A


Computer Science Workshop : Grade 12
SQL
(Questions from CBSE Board Exam- from 1998
to 2010)
YEAR: 1998(Outside Delhi)
Q 5 (a) What is need for normalization? Define
First, Second and Third Normal Form.
Ans. Need for Normalization: The basic
objective of normalization is to reduce
redundancy, which means
that data to be stored only once. Because storing
data many times lead to wastage of space and
increase
access time. So relation/table are to be
normalized so that we can alter them at any time
without doing
much changes.
First Normal Form: A table / relation is said to be
in First Normal Form (1NF) if and only if all
underlying domains of the relation contain
atomic (individual) values.
Second Normal Form: A table / relation is said to
be in Second Normal Form (2NF) if and only if it
is in
First Normal Form and every non-key attribute is
functionally dependent on the Primary Key.
Third Normal Form: A table / relation is said to
be in Third Normal Form (1NF) if and only if it
is in
Second Normal From and every non-key
attribute is non-transitively dependent upon the
primary key.
NOTE : Write SQL commands for(b) to (g) and
write the output for (h) on the basis of table
HOSPITAL.
TABLE : HOSPITAL
No Name Age Department Datofadm Charges
Sex
1 Sandeep 65 Surgery 23/02/98 300 M
2 Ravina 24 Orthopedic 20/01/98 200 F
3 Karan 45 Orthopedic 19/02/98 200 M
4 Tarun 12 Surgery 01/01/98 300 M
5 Zubin 36 ENT 12/02/98 250 M
6 Ketaki 16 ENT 24/02/98 300 F
7 Ankita 29 Cardiology 20/02/98 800 F
8 Zareen 45 Gynecology 22/02/98 300 F
9 Kush 19 Cardiology 13/01/98 800 M
10 Shaliya 31 Nuclear
Medicine
19/02/98 400 MfF
No Name Age Department DateOfAdm Charge Sex
s
1 Sandeep 65 Surgery 23/02/98 300 M
2 Ravina 24 Orthopedic 20/01/98 200 F
3 Karan 45 Orthopedic 19/02/98 200 M
4 Tarun 12 Surgery 01/01/98 300 M
5 Zubin 36 ENT 12/02/98 250 M
6 Ketaki 16 ENT 24/02/98 300 F
7 Ankita 29 Cardiology 20/02/98 800 F
8 Zareen 45 Gynecology 22/02/98 300 F
9 Kush 19 Cardiology 13/01/98 800 M
10 Shaliya 31 Nuclear Medicine 19/02/98 400 M

5- OUR OWN HIGH SCHOOL, AL


WARQA’A
6- Computer Science Workshop : Grade 12
7- SQL
8- (Questions from CBSE Board Exam- from
1998 to 2010)
9- YEAR: 1998(Outside Delhi)
10- Q 5 (a) What is need for normalization?
Define First, Second and Third Normal
Form.
11- Ans. Need for Normalization: The basic
objective of normalization is to reduce
redundancy, which means
12- that data to be stored only once. Because
storing data many times lead to wastage of
space and increase
13- access time. So relation/table are to be
normalized so that we can alter them at any
time without doing
14- much changes.
15- First Normal Form: A table / relation is said
to be in First Normal Form (1NF) if and only
if all
16- underlying domains of the relation contain
atomic (individual) values.
17- Second Normal Form: A table / relation is
said to be in Second Normal Form (2NF) if
and only if it is in
18- First Normal Form and every non-key
attribute is functionally dependent on the
Primary Key.
19- Third Normal Form: A table / relation is said
to be in Third Normal Form (1NF) if and
only if it is in
20- Second Normal From and every non-key
attribute is non-transitively dependent upon
the primary key.
21- NOTE : Write SQL commands for(b) to (g)
and write the output for (h) on the basis of
table HOSPITAL.
22- TABLE : HOSPITAL
23- No Name Age Department Datofadm
Charges Sex
24- 1 Sandeep 65 Surgery 23/02/98 300 M
25- 2 Ravina 24 Orthopedic 20/01/98 200 F
26- 3 Karan 45 Orthopedic 19/02/98 200 M
27- 4 Tarun 12 Surgery 01/01/98 300 M
28- 5 Zubin 36 ENT 12/02/98 250 M
29- 6 Ketaki 16 ENT 24/02/98 300 F
30- 7 Ankita 29 Cardiology 20/02/98 800 F
31- 8 Zareen 45 Gynecology 22/02/98 300 F
32- 9 Kush 19 Cardiology 13/01/98 800 M
33- 10 Shaliya 31 Nuclear
34- Medicine
35- 19/02/98 4
(a) (b) To show all information about the
patients of cardiology department.
(b) Ans: SELECT * FROM hospital WHERE
department=’Cardiology’;
(c) (c ) To list the names of female patients
who are in orthopedic dept.
(d) Ans: SELECT name FROM hospital
WHERE sex=’F’ AND
department=’Orthopedic’;
(e) (d) To list names of all patients with their
date of admission in ascending order.
(f) Ans.: SELECT name, dateofadm FROM
hospital ORDER BY dateofadm;
(g) (e) To display Patient’s Name, Charges, age
for male patients only.
(h) Ans: SELECT name, charges, age FROM
hospital WHERE sex=’M’;
(i) (f) To count the number of patients with age
>20.
(j) Ans.: SELECT COUNT(age) FROM
hospital WHERE age>20;
(k) (g) To insert a new row in the HOSPITAL
table with the following
(a) To show all information about the patients of cardiology department.
(b) To list the names of female patients who are in orthopedic dept
(c) To list the names of female patients who are in orthopedic dept
(d) To display Patient’s Name, Charges, age for male patients only
(e) To count the number of patients with age >20.
(f) To insert a new row in the HOSPITAL table with the following
(g) OUR OWN HIGH SCHOOL, AL WARQA’A
(h) Computer Science Workshop : Grade 12
(i) 11,”mustafa”,37,”ENT”,(25/02/98},250,”M
(j) OUR OWN HIGH SCHOOL, AL WARQA’A
(k) Computer Science Workshop : Grade 12
(l) 11,”mustafa”,37,”ENT”,(25/02/98},250,”M
(m) OUR OWN HIGH SCHOOL, AL WARQA’A
(n) Computer Science Workshop : Grade 12
(o) 11,”mustafa”,37,”ENT”,(25/02/98},250,”M
1,”mustafa”,37,”ENT”,(25/02/98},250,”M”

Write the output of the following queries:


i. Select COUNT(distinct departments) from HOSPITAL;
ii. Select Max (Age) from HOSPITAL where SEX = “M”;
iii. Select AVG(Charges) from HOSPITAL where SEX = “ F”;
iv. Select SUM(Charges) from HOSPITAL where Datofadm<{12/02/98}

ANSWERS

(a) SELECT * FROM hospital WHERE department=’Cardiology’;


(b) SELECT name FROM hospital WHERE sex=’F’ AND department=’Orthopedic’;
(c) SELECT name, dateofadm FROM hospital ORDER BY dateofadm;
(d) SELECT name, charges, age FROM hospital WHERE sex= ‘M’;
(e) SELECT COUNT(age) FROM hospital WHERE age>20;
(f) INSERT INTO hospital VALUES (11, ‘Mustafa’, 37, ‘ENT’, ‘25/02/98’, 250, ‘M’);

I.

Count(Distinct(Department))
6
II.

Max(AGE)
65
III.

AVG(Charges)
400
IV.

Sum(Charges)
1300

5- Write the SQL commands for the table Employee:


(a) To show all information about the
employees of Engg. Branch
(b) Ans. SELECT * FROM employee
WHERE branch= ‘Engg.’;
(c) To show all information about the
employees of Engg. Branch
(d) Ans. SELECT * FROM employee
WHRE branch= ‘Engg.’;
(a) To show all information about the employees of Engg. Branch
(b) To show the average age of all the males.
(c) To list the names of female employees who are in Science branch
(d) To list the names of all employees with their date of retirement in ascending
order
(e) To display Employee’s name , Salary ,Age for male employees only
(f) To count the number of employees with AGE > 33.
(g) To insert a new row in the EMPLOYEE table with the following data
9,”Rohit”,46,”language”,{22/06/98},2300,”M”.

6- Write the SQL commands for the Following:


a) List the names of those students who obtained DIV 1 sorted by NAME
b) )Display a report, listing NAME , STIPEND , SUBJCT and amount of stipend
received in a year assuming that the STIPEND is paid every month.
c) To display the total stepinds from the Graduate table where name starts with 'R'.

Write the Output for the following queries:


d) Select MIN(AVERAGE ) from GRADUATE where SUBJECT=”PHYSICS”;
e) Select SUM(STIPEND) from GRADUATE where DIV=1;
f) Select AVG(STIPEND) from GRADUATE where AVERAGE >=65;
g) Select COUNT( distinct SUBJECT) from GRADUATE;

7- Write Queries for the Following from the SPORTS table:

(a) Display the names of the students who have grade ‘C’ in either Game1 or Game2
or both.
(b) Display the number of students getting grade ‘A’ in Cricket.
(c) Display the names of the students who have same game for both Game1 and
Game2.
(d) Display the games taken up by the students , whose name starts with ‘A’.
(e) Add a new column named ‘ Marks’.
(f) Assign a value 200 for marks for all those who are getting grade ‘B’ or grade ‘ A’ in
both Game1 and Game2.
(g) Arrange the whole table in the alphabetical order of Name.
8- Given the following Teacher relation : Write SQL command for the
questions :

i. To select all the information of teacher in computer department.


ii. To select all the information of teacher in computer department.
iii. To list all names of teachers with date of admission in ascending order.
iv. To select the person with the least experience.
v. TO display Teacher’s name, Department, and Salary of female teacher.
vi. To count the number of items whose salary is less than 10,000.

Give the output of the following SQL command :


vii. .SELECT MIN(DISTINCT Salary) FROM Teacher;
viii. SELECT MIN (Salary) FROM Teacher WHERE Sex = ”M”;
ix. SELECT SUM(Salary) FROM Teacher WHERE Department = ”HISTORY”;
x. SELECT AVG(Salary) FROM Teacher WHERE Dateofjoining < {1/1/98};
9- Write the commands for the given question of the table FURNITURE:
(a) To show all information about the Baby cots from the FURNITURE table.
(b) To list the ITEMNAME which are priced at more than 15000 from the FURNITURE
table.
(c) To list ITEMNAME and TYPE of those items, in which date of stock is before
22/01/02 from the FURNITURE table in the descending order of ITEMNAME.
(d) To list the types of furniture stocked in February.
(e) To list how much total discount is on Baby Cot.
(f) To find the total price of the Items whose name starts with 'D'.

10- Predict the output of the following:


(a) select round (29.21) ;
(b) select round (32.76, 1) ;
(c) select mid("preoccupied ", 4) ;
(d) select dayofmonth(' 2009-02-03 ') ;
(e) select dayofyear(' 2009-02-03 ') ;
(f) select monthname(' 2020-05-17 ') ;
(g) select sign(-56299) ;
(h) select mod(50, 7) ;
(i) select length('supreme') ;
(J) select concat(concat('John', 'is a ') mechanic) ;
(k) select power(2, 9) ;
(l) select round(896729, -4) ;
(m) select truncate(896729, -4) ;

xi. OUR OWN HIGH SCHOOL, AL WARQA’A


xii. Computer Science Workshop : Grade 12
xiii. Ans.: SELECT * FROM teacher WHERE
department= ‘Computer’;
xiv. (d) To list the name of female teachers in
History department.
xv. Ans.: SELECT * FROM teacher WHWRE
sex= ‘F’ AND department = ‘History’;
xvi. (e) To list all names of teachers with date of
admission in ascending order.
xvii. Ans.: SELECT name, dateofjoining FROM
teacher ORDER BY dateofjoining;

You might also like