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

SQL

1. The document contains 16 SQL queries that retrieve and analyze data from student, enrollment, batch, and course tables. 2. The queries display student data based on name, age, date of birth, email, city, and other fields. They also analyze enrollment data, batch details, and calculate totals. 3. Aggregate functions like COUNT, SUM, DATE_ADD are used along with JOINs, WHERE clauses, GROUP BY and other SQL clauses.

Uploaded by

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

SQL

1. The document contains 16 SQL queries that retrieve and analyze data from student, enrollment, batch, and course tables. 2. The queries display student data based on name, age, date of birth, email, city, and other fields. They also analyze enrollment data, batch details, and calculate totals. 3. Aggregate functions like COUNT, SUM, DATE_ADD are used along with JOINs, WHERE clauses, GROUP BY and other SQL clauses.

Uploaded by

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

1) Display all undergraduate student whose name starts with S and is of lengt

h between 5 and 20.


mysql> select sname ,squal from
-> student
-> where squal='undergraduate'
-> and
-> sname like 's%'
-> and
-> length(sname) between 5 and 20;
+--------+---------------+
| sname | squal |
+--------+---------------+
| suman | undergraduate |
| sonali | undergraduate |
+--------+---------------+
2) Display the student who are senior citizen (>=60).
mysql> select sname as senior_citizen,(extract(year from curdate())-extract(year
from sdob)) as age
-> from student
-> where
-> (extract(year from curdate())-extract(year from sdob))>=60;
+----------------+------+
| senior_citizen | age |
+----------------+------+
| john | 64 |
| jadeja | 73 |
| amir | 68 |
+----------------+------+
another method srijan method
mysql> select * from student
-> where datediff(date_sub(curdate(), interval 60 year),sdob)>=0;
+------+--------+------------+-----------+--------------+-----------------------
-+-------------+
| sid | sname | sdob | scity | squal | semail
| sphone |
+------+--------+------------+-----------+--------------+-----------------------
-+-------------+
| s002 | john | 1949-01-07 | hyderabad | postgraduate | [email protected]
| 9833978933 |
| s005 | jadeja | 1940-01-23 | kolkata | postgraduate | [email protected]
| 09837865432 |
| s014 | amir | 1945-01-13 | delhi | postgraduate | [email protected]
| 29830978900 |
+------+--------+------------+-----------+--------------+-----------------------
3) Display student who were born after 1st of June 1980.
mysql> select sname ,sdob
-> from student
-> where(sdob>'1980-06-01');
+--------+------------+
| sname | sdob |
+--------+------------+
| rajesh | 1980-12-17 |
| maya | 1990-12-17 |
| suman | 1995-06-17 |
| soha | 1990-07-17 |
| thapa | 1980-08-17 |
| amir | 1992-01-01 |
| ramesh | 1980-12-17 |
| esha | 1981-10-30 |
| sonali | 1995-11-11 |
| lisa | 1983-01-31 |
| smith | 1980-12-17 |
| rajesh | 1994-07-08 |
+--------+------------+
another srijan method
select * from student
where datediff(sdob,'1980-06-01')>0;
4) The student are suppose to only provide mobile numbers .
All mobile numbers should start with zero followed by 10 digits. Display
student name having invalid phone numbers.
mysql> select sname,sphone
-> from student
-> where sphone not like '0__________';
+-------+-------------+
| sname | sphone |
+-------+-------------+
| john | 9833978933 |
| suman | 0983097890 |
| thapa | 19830978900 |
| amir | 29830978900 |
+-------+-------------+
5) All emails should have @ anywhere after the first character and should end
with .com.
Display count of students having invalid email id.
mysql> select count(semail)
-> from student
-> where
-> semail not like'_%@%.com';
+---------------+
| count(semail) |
+---------------+
| 2 |
+---------------+
6) Display the name and email of student who have a Gmail account.
mysql> select sname,semail
-> from student
-> where
-> semail like'%gmail%';
+-----------+------------------+
| sname | semail |
+-----------+------------------+
| rajesh | [email protected] |
| kunal | [email protected] |
| suman | [email protected] |
| thapa | [email protected] |
| hira | [email protected] |
| akash | [email protected] |
| amir | amirgmail.com |
| suresh | [email protected] |
| esha | [email protected] |
| gopichand | [email protected] |
| sonali | [email protected] |
| lisa | [email protected] |
| rajesh | [email protected] |
+-----------+------------------+
7) Display the above record but do not consider invalid email id.
mysql> select sname,semail
-> from student
-> where
-> semail like'%@gmail%';
+-----------+------------------+
| sname | semail |
+-----------+------------------+
| rajesh | [email protected] |
| kunal | [email protected] |
| suman | [email protected] |
| thapa | [email protected] |
| hira | [email protected] |
| akash | [email protected] |
| suresh | [email protected] |
| esha | [email protected] |
| gopichand | [email protected] |
| sonali | [email protected] |
| lisa | [email protected] |
| rajesh | [email protected] |
+-----------+------------------+
8) Display the qualification and the total number of students hired based o
n their qualifications.
(Alias use totalStud for total number of students)
mysql> select squal,count(sid) as totalstud
-> from student
-> group by squal;
+---------------+-----------+
| squal | totalstud |
+---------------+-----------+
| graduate | 9 |
| postgraduate | 7 |
| undergraduate | 4 |
+---------------+-----------+
9) Display the full name of the month and the total number of students who
are having their birthday in that month.
(Alias use Month and Total)
mysql> select date_format(sdob,'%M') as month,count(sid) as total
-> from student
-> group by month;
+-----------+-------+
| month | total |
+-----------+-------+
| August | 1 |
| December | 4 |
| February | 1 |
| January | 6 |
| July | 2 |
| June | 1 |
| March | 1 |
| May | 1 |
| November | 1 |
| October | 1 |
| September | 1 |
+-----------+-------+
10) Display the student name that was born in a leap year ordering by studen
t name and year of birth.
mysql> select sname,sdob
-> from student
-> where
-> (
-> (((extract(year from sdob))%400)=0
-> and ((extract(year from sdob))%100)=0)
-> or
-> (
-> ((extract(year from sdob))%4)=0
-> and
-> ((extract(year from sdob))%100)<>0
-> ))
-> order by sname,sdob;
+--------+------------+
| sname | sdob |
+--------+------------+
| amir | 1992-01-01 |
| jadeja | 1940-01-23 |
| rajesh | 1980-12-17 |
| ramesh | 1980-12-17 |
| smith | 1980-12-17 |
| suresh | 1980-03-22 |
| thapa | 1980-08-17 |
+--------+------------+
11) Display student whose city is Kolkata as HomeStudent and others as Dista
nceStudent under a column Remarks.
Also display the name and city of the student.
mysql> select sname,
-> case scity
-> when 'kolkata' then 'hometown'
-> else 'distance_student'
-> end as remarks
-> from student;
+-----------+------------------+
| sname | remarks |
+-----------+------------------+
| rajesh | hometown |
| john | distance_student |
| kunal | distance_student |
| maya | hometown |
| jadeja | hometown |
| suman | hometown |
| soha | distance_student |
| thapa | distance_student |
| hira | distance_student |
| akash | hometown |
| amir | distance_student |
| ramesh | hometown |
| suresh | hometown |
| amir | distance_student |
| esha | distance_student |
| gopichand | distance_student |
| sonali | distance_student |
| lisa | distance_student |
| smith | distance_student |
| rajesh | distance_student |
+-----------+------------------+
12) Display batchid, coursename, batch start date, batch end date for all ba
tches.
(batch end date=batch start date +course duration)
mysql> select b.batchid,c.coursename,b.bsdate,date_add(bsdate,interval coursedur
ation hour) as enddate
-> from batch b inner join course c
-> on b.courseid=c.courseid;
+---------+------------+---------------------+---------------------+
| batchid | coursename | bsdate | enddate |
+---------+------------+---------------------+---------------------+
| b001 | sql server | 2013-02-01 09:30:00 | 2013-02-03 01:30:00 |
| b009 | sql server | 2013-02-16 09:30:00 | 2013-02-18 01:30:00 |
| b002 | compmat | 2013-03-01 09:30:00 | 2013-03-06 09:30:00 |
| b006 | compmat | 2013-01-27 09:30:00 | 2013-02-01 09:30:00 |
| b008 | compmat | 2013-01-28 09:30:00 | 2013-02-02 09:30:00 |
| b003 | biomaths | 2013-01-01 09:30:00 | 2013-01-08 01:30:00 |
| b004 | biomaths | 2013-03-31 09:30:00 | 2013-04-07 01:30:00 |
| b010 | biomaths | 2012-12-12 09:30:00 | 2012-12-19 01:30:00 |
| b007 | word | 2012-11-30 09:30:00 | 2012-11-30 17:30:00 |
| b005 | photo | 2013-04-04 09:30:00 | 2013-04-04 17:30:00 |
+---------+------------+---------------------+---------------------+
13) Display all batchid having a difference of 10 hours and less between its
starting and ending date.
mysql> select batchid
-> from batch inner join course
-> using(courseid)
-> where courseduration<=10;
+---------+
| batchid |
+---------+
| b007 |
| b005 |
+---------+
another srijan method
select batchid
from batch b, course c
where b.courseid=c.courseid
and
datediff(date_add(bsdate,interval courseduration hour),date_add(bsdate,interva
l 10 hour))<=0;
14) Display all batches having similar start date and strength.
mysql> select * from batch
-> t1, batch t2 where(t1.batchid<>t2.batchid and t1.bsdate=t2.bsdate and t1.
bstrength=t2.bstrength);
Empty set (0.00 sec)
15) Display student who enrolled for the batch after its start date.
mysql> select s.sid,s.sname
-> from student s inner join enrollment e
-> on
-> s.sid=e.sid
-> inner join batch b
-> on e.batchid=b.batchid
-> where
-> datediff(b.bsdate,e.edate)<0;
+------+--------+
| sid | sname |
+------+--------+
| s004 | maya |
| s007 | soha |
| s008 | thapa |
| s009 | hira |
| s018 | lisa |
| s019 | smith |
| s020 | rajesh |
+------+--------+
16) Display the studentid, studentname , totalfees for all student
mysql> select s.sid,s.sname,sum(c.coursefees) as total_fees
-> from student s inner join enrollment
-> using(sid)
-> inner join batch
-> using(batchid)
-> inner join course c
-> using(courseid)
-> group by s.sid;
+------+-----------+------------+
| sid | sname | total_fees |
+------+-----------+------------+
| s001 | rajesh | 10300.00 |
| s002 | john | 8500.00 |
| s003 | kunal | 13300.00 |
| s004 | maya | 2500.00 |
| s005 | jadeja | 4000.00 |
| s006 | suman | 8800.00 |
| s007 | soha | 9500.00 |
| s008 | thapa | 8000.00 |
| s009 | hira | 14300.00 |
| s010 | akash | 8500.00 |
| s011 | amir | 7000.00 |
| s012 | ramesh | 7000.00 |
| s013 | suresh | 8000.00 |
| s014 | amir | 10000.00 |
| s016 | gopichand | 3000.00 |
| s017 | sonali | 10000.00 |
| s018 | lisa | 4000.00 |
| s019 | smith | 7000.00 |
| s020 | rajesh | 7000.00 |
+------+-----------+------------+
17) Display courses which are not being taught currently along with courses
which are being taught.
Also display the batchid for the courses currently running and null for non exec
uting courses.
srijan method
select c.courseid,c.coursename,'null' batchid from batch b,course c where
b.courseid=c.courseid and datediff(date_add(b.bsdate,interval c.courseduration
hour), curdate())<0
union
select c.courseid,c.coursename,b.batchid from batch b,course c where b.courseid=
c.courseid and datediff(date_add(b.bsdate,interval c.courseduration day),
curdate())>0;
18) Display count of students having no contact information. (Either email o
r phone).
mysql> select count(sid)
-> from student
-> where
-> sphone is null
-> or
-> semail is null;
+------------+
| count(sid) |
+------------+
| 2 |
+------------+
19) Display coursename having above average fees.
mysql> select coursename
-> from course
-> where
-> coursefees>(select avg(coursefees) from course);
+------------+
| coursename |
+------------+
| compmat |
| biomaths |
+------------+
20) Display coursename where fees are less than the average fees of its cate
gory.
mysql> select c1.coursename from course c1 where c1.coursefees<
-> (
-> select avg(coursefees)
-> from course c2
-> where c1.coursecategory=c2.coursecategory
-> );
+------------+
| coursename |
+------------+
| word |
+------------+
21) Display the coursename having the highest enrollment.
mysql> select c.coursename
-> from course c inner join batch b
-> using(courseid)
-> inner join enrollment e
-> using(batchid)
-> group by coursename
-> order by count(e.sid) desc limit 1;
+------------+
| coursename |
+------------+
| compmat |
+------------+
22) Display student name having duplicate email ids.
mysql> select s1.sname,s1.semail
-> from student
-> s,student s1
-> where s.semail=s1.semail
-> and s.sid<>s1.sid;
+--------+------------------+
| sname | semail |
+--------+------------------+
| rajesh | [email protected] |
| rajesh | [email protected] |
+--------+------------------+
23) Display student name having similar name but different email ids
mysql> select s1.sname,s1.semail
-> from student
-> s,student s1
-> where s.sid<>s1.sid
-> and s.sname=s1.sname
-> and s.semail<>s1.semail;
+-------+------------------------+
| sname | semail |
+-------+------------------------+
| amir | amirgmail.com |
| amir | [email protected] |
+-------+------------------------+

You might also like