SQL速成 Crash Course

All you need is a good tutorial like this one here, which you can finish in 2 hrs.

And find yourself a good workbook to go through. Click here to download the SQL worksheet I’ve worked on. Many thanks to python4csip.com.

Tables to work with

student

Idstu_namestipendsujetaverage
1KARAN400PHYSICS68
2DIVAKAR450COMP SC68
3DIVYA400CHEMISTRY62
4ARUN350PHYSICS63
5SABINA500MATHS70
6JOHN550COMP SC55
7ROBERT300CHEMISTRY64
8RUBINA250PHYSICSNULL
9VIKAS400HISTORY62
10MOHAN450MATHS57
11DICK400CHEMISTRY89

guide

sujetADVISOR
PHYSICSALOK
COMP SCRAJAN
CHEMISTRYMANJU
MATHSSMITA
HISTORYKISHORE

Simple exercises (10)

Retrieve the names and stipends of all students.

select stu_name, stipend
from student;

Find the average stipend of all students.

select avg(stipend)
from student;

Get the names and subjects of students who have an average score of 65 or above.

select stu_name, sujet
from student
where average >= 65;

List the subjects and their corresponding advisors from the GUIDE table.

select sujet, advisor
from guide;

Display the names of students along with their advisor’s name for the students who have an advisor.

select stu_name, advisor
from student s
join advisor g on s.sujet = g.sujet
where g.sujet is not null;

Find the subjects for which there are no students.

select g.sujet
from guide g
left join student s on g.sujet = s.sujet
where s.sujet is null;

Get the names of students who have a NULL value for the ‘average’ column.

select stu_name
from student
where average is null;

Update the stipend of the student with Id 5 to 550.

update student
set stipend = 550
where id = 5;

Delete all records from the GUIDE table where the advisor is ‘MANJU’.

delete from guide
where advisor = 'MANJU'

Count the number of students in each subject.

select sujet, count(*)
from student
group by sujet;

Intermediate exercises (9)

Find the students who have the highest average score in each subject.

select *
from student
having average in (
	select max(average)
	from student
	group by sujet);

Calculate the percentage of students in each subject who have a stipend greater than the average stipend of that subject.

select s.sujet, (count(case when s.stipend > avg_suj_stipend then 1 end) /count(*) * 100) as percentage
(select count(*)
from student s
join (select sujet, avg(stipend) as avg_suj_stipend
from student
group by sujet) as subquery on subquery.sujet = s.sujet
group by sujet;

Retrieve the subjects where all students have a stipend greater than 300.

select sujet
from student
group by sujet
having min(stipend) > 300;

Calculate the difference between the highest and lowest stipend for each subject.

select sujet, (max(stipend)-min(stipend)) as difference
from student
group by sujet;

(6)Retrieve the names of students who have a stipend greater than their advisor’s average stipend.

SELECT s.stu_name
FROM student s
JOIN GUIDE g ON s.sujet = g.sujet
WHERE s.stipend > (
    SELECT AVG(stipend)
    FROM student
    WHERE sujet = g.sujet
);

Calculate the average stipend for each subject, excluding subjects where the average stipend is below 400.

select avg(stipend) as avg_stipend
from student
group by sujet
where avg_stipend >= 400;

Retrieve the names of students who have the same advisor as the student with Id 3, but a higher stipend.

select s.stu_name
from student s
join guide g on s.sujet = g.sujet
where s.id <> 3
and s.stipend > (select stipend from student where id=3)
and advisor = (select g.advisor from student s
join guide g on s.sujet = g.sujet where s.id=3);

##alternatively

SELECT s1.stu_name
FROM student s1
JOIN GUIDE g1 ON s1.sujet = g1.sujet
JOIN student s2 ON s2.sujet = g1.sujet
WHERE s2.Id = 3
AND s1.stipend > s2.stipend;

Find the subject(s) where the number of students is greater than the number of advisors.

SELECT sujet
FROM student
GROUP BY sujet
HAVING COUNT(*) > (SELECT COUNT(DISTINCT advisor) FROM GUIDE);

Calculate the average stipend for students in subjects where the average stipend is above the overall average stipend of all subjects.

select sujet, avg(stipend) as avg_stipend
from student
group by sujet
where avg_stipend > avg(stipend)

Hard Exercises (10)

Find the subject(s) with the highest average stipend among the students.

select sujet
from student
group by sujet
having avg(stipend) = (
	select max(avg_stipend)
    from (select avg(stipend) as avg_stipend
			from student
            group by sujet) as subquery
	);

Retrieve the names of students who have the same advisor as the student with Id 2.

select s.stu_name
from student s
join guide g on s.sujet = g.sujet
where s.id <> 2
and g.advisor = (
	select g.advisor
    from student s
	join guide g on s.sujet = g.sujet
    where s.id = 2);
    
##alternatively

SELECT s1.stu_name
FROM student s1
JOIN GUIDE g1 ON s1.sujet = g1.sujet
JOIN GUIDE g2 ON g1.advisor = g2.advisor
JOIN student s2 ON s2.sujet = g2.sujet
WHERE s2.Id = 2
AND s1.Id <> 2;

Calculate the average stipend for each subject, ordered in descending order of the average stipend.

select sujet, avg(stipend) as avg_stipend
from student
group by sujet
order by avg_stipend DESC;

Identify the student(s) who have a stipend greater than the average stipend of all students.

select *
from student
where stipend > (
	select avg(stipend)
    from student);

List the subjects and the count of students for each subject, including subjects with no students.

select g.sujet, count(s.id)
from guide g 
left join student s on g.sujet = s.sujet
group by sujet;

Find the advisor(s) who advise the maximum number of students.

select g.advisor
from guide g join student s on g.sujet=s.sujet
group by g.advisor
having count(s.id) = (
	select max(stu_count)
    from (
		select count(s.id) as stu_count
        from student s join guide g on s.sujet=g.sujet
        group by g.advisor) as subquery
	);

Retrieve the student(s) with the highest stipend in each subject.

select * 
from student s
join
(select sujet, max(stipend) as max_stipend
from student
group by sujet) as subquery
on s.sujet = subquery.sujet
where s.stipend = max_stipend;

Calculate the average stipend for each advisor, including advisors with no students.

select g.advisor, avg(stipend) as avg_stipend
from guide g 
left join student s on g.sujet = s.sujet
group by g.advisor;

Identify the subject(s) where the maximum stipend is greater than 500.

select sujet
from student
group by sujet
having max(stipend) > 500;

Find the student(s) who have the highest stipend in their subject, but their average score is below 70.

select s.*
from student s
join
(select sujet, max(stipend) as maximum_stipend
from student
group by sujet) as subquery on s.sujet = subquery.sujet
where s.stipend = maximum_stipend
and s.average < 70;

46 responses

  1. Amanda4846 Avatar
    Amanda4846

    Promote, refer, earn—join our affiliate program now! https://shorturl.fm/v1h60

  2. Jayden3911 Avatar
    Jayden3911

    Unlock exclusive rewards with every referral—enroll now! https://shorturl.fm/3A6jo

  3. Rachel20 Avatar
    Rachel20

    Boost your income—enroll in our affiliate program today! https://shorturl.fm/b8vb1

  4. Theresa899 Avatar
    Theresa899

    Become our affiliate and watch your wallet grow—apply now! https://shorturl.fm/diN7Y

  5. Cornelius2200 Avatar
    Cornelius2200

    Drive sales, earn commissions—apply now! https://shorturl.fm/mhQh7

  6. Cora2949 Avatar
    Cora2949

    Grow your income stream—apply to our affiliate program today! https://shorturl.fm/GOnM7

  7. Tom4218 Avatar
    Tom4218

    Monetize your audience—become an affiliate partner now! https://shorturl.fm/wC0A0

  8. Duane209 Avatar
    Duane209

    Partner with us and earn recurring commissions—join the affiliate program! https://shorturl.fm/609A3

  9. Briley521 Avatar
    Briley521

    Apply now and receive dedicated support for affiliates! https://shorturl.fm/SurTV

  10. Isabella4556 Avatar
    Isabella4556

    Monetize your audience—become an affiliate partner now! https://shorturl.fm/ahg3i

  11. Haiden336 Avatar
    Haiden336

    Monetize your audience with our high-converting offers—apply today! https://shorturl.fm/JKarv

  12. Erika3029 Avatar
    Erika3029

    Tap into unlimited earnings—sign up for our affiliate program! https://shorturl.fm/3M3VU

  13. Bruce3993 Avatar
    Bruce3993

    Join our affiliate program and start earning today—sign up now! https://shorturl.fm/MvtHW

  14. Maddison719 Avatar
    Maddison719

    Promote our brand, reap the rewards—apply to our affiliate program today! https://shorturl.fm/ehPi0

  15. Matthias1520 Avatar
    Matthias1520

    Drive sales, earn commissions—apply now! https://shorturl.fm/SZfXa

  16. Annabelle3081 Avatar
    Annabelle3081

    Promote our brand and watch your income grow—join today! https://shorturl.fm/KwGwq

  17. Angelica4732 Avatar
    Angelica4732

    Apply now and unlock exclusive affiliate rewards! https://shorturl.fm/cL6z1

  18. Joe3861 Avatar
    Joe3861

    Start profiting from your network—sign up today! https://shorturl.fm/9VjVp

Leave a Reply to Harrison945 Cancel reply

Your email address will not be published. Required fields are marked *

Back to top arrow