Chapter-4
SQL(structured queries language)
SQL-1.
(i). Display the Mobile company, Mobile name & price in descending order
of the manufacturing date.
: SELECT M_Compnay, M_Name, M_Price FROM MobileMaster
ORDER BY M_Mf_Date DESC;
(ii). List the details of mobile whose name starts with “S”.
: SELECT * FROM MobileMaster
WHERE M_Name LIKE “S%‟;
(iii). Display the Mobile supplier & quantity of all mobiles except “MB003‟
: SELECT M_Supplier, M_Qty FROM MobileStock
WHERE M_Id <>”MB003”;
(iv). To display the name of mobile company having price between 3000 &
5000.
: SELECT M_Company FROM MobileMaster
WHERE M_Price BETWEEN 3000 AND 5000;
(v) SELECT M_Id, SUM(M_Qty) FROM MobileStock GROUP BY M_Id;
MB004 450
MB003 400
MB003 300
MB003 200
(vi) SELECT MAX(M_Mf_Date), MIN(M_Mf_Date) FROM MobileMaster;
: 2017-11-20 2010-08-21
(vii) SELECT M1.M_Id, M1.M_Name, M2.M_Qty, M2.M_Supplier FROM MobileMaster
M1, MobileStock M2 WHERE M1.M_Id=M2.M_Id AND M2.M_Qty>=300;
MB004 Unite3 450 New_Vision
MB001 Galaxy 300 Classic Mobile Store
(viii) SELECT AVG(M_Price) FROM MobileMaster;
: 5450
SQL-2
· QUERIES
· Display the Trainer Name, City & Salary in descending order of theirHiredate.
: SELECT TNAME, CITY, SALARY FROM TRAINER
ORDER BY HIREDATE;
· To display the TNAME and CITY of Trainer who joined the Institute in
the month of December 2001
:SELECT TNAME, CITY FROM TRAINER
WHERE HIREDATE BETWEEN ‘2001-12-01’
AND ‘2001-12-31’;
· To display TNAME, HIREDATE, CNAME, STARTDATE from tables TRAINER and
COURSE of all those courses whose FEES is less than or equal to 10000.
: SELECT TNAME,HIREDATE,CNAME,STARTDATE FROM TRAINER,
COURSE
WHERE TRAINER.TID=COURSE.TID AND FEES<=10000;
· To display number of Trainers from each city.
: SELECT CITY, COUNT(*) FROM TRAINER
GROUP BY CITY;
· SELECT TID, TNAME, FROM TRAINER WHERE CITY NOT IN(‘DELHI’, ‘MUMBAI’);
: 103 DEEPTI
106 MANIPRABHA
· SELECT DISTINCT TID FROM COURSE;
: 101
103
102
104
105
· SELECT TID, COUNT(*), MIN(FEES) FROM COURSE GROUP BY TID HAVING
COUNT(*)>1;
: 101 2 12000
· SELECT COUNT(*), SUM(FEES) FROM COURSE WHERE STARTDATE< ‘2018-
09-15’;
:4 65000
SQL-3
· To display details of those Faculties whose salary is greater than 12000.
: Select * from faculty
where salary > 12000;
· To display the details of courses whose fees is in the range of 15000 to 50000
(both values included).
: Select * from Courses
where fees between 15000 and 50000;
iii ) To increase the fees of all courses by 500 of “System Design” Course.
: Update courses set fees = fees + 500
where Cname = “System Design”;
(iv) To display details of those courses which are taught by ‘Sulekha’ in descending
order of courses.
: Select * from faculty,courses
where faculty.f_id = course.f_id and fac.fname =
'Sulekha'
order by cname desc;
v) Select COUNT(DISTINCT F_ID) from COURSES ;
:4
vi) Select MIN(Salary) from FACULTY,COURSES where COURSES.F_ID =
FACULTY.F_ID;
:6000
vii) Select avg(fees) from COURSES;
:17500
SQL-4
· To display all the details of those watches whose name ends
with ‘Time’
: select * from watches
where watch_name like ‘%Time’;
ii. To display watch’s name and price of those watches which have
price range in between 5000-15000.
: select watch_name, price from watches
where price between 5000 and 15000;
· To display total quantity in store of Unisex type watches.
: select sum(qty_store) from watches where type like ’Unisex’;
· To display watch name and their quantity sold in first quarter
: select watch_name,qty_sold from watches w,sale s
where w.watchid=s.watchid and quarter=1;.
v. select max(price), min(qty_store) from watches;
: 25000 100
· select quarter, sum(qty_sold) from sale group by quarter;
:1 15
2 30
· 45
· 15
vii. select watch_name,price,type from watches w, sales where
w.watchid!=s.watchid;
: HighFashion 7000 Unisex
viii. select watch_name, qty_store, sum(qty_sold),
qty_store- sum(qty_sold) “Stock” from watches w, sale s
where w.watchid=s.watchid group by s.watchid;
: HighTime 100 25 75
LifeTime 150 40 110
Wave 200 30 170
Golden Time 100 10 90
PROGRAM 3-'To sort particular column in one order:'
SELECT BusinessEntityID, FirstName, LastName, ModifiedDate
FROM [Person].[Person]
ORDER BY [ModifiedDate] DESC;
OUTPUT: