0% found this document useful (0 votes)
1 views21 pages

Lab Problem 4

The document contains SQL queries related to a university database, including finding departments with higher budgets than Astronomy, listing instructors with the number of sections taught, and identifying students who retook courses multiple times. It also includes queries for instructor salaries, department budgets, and average salaries across departments. Additionally, the document provides information on courses taught in specific semesters and the number of instructors in each department.
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)
1 views21 pages

Lab Problem 4

The document contains SQL queries related to a university database, including finding departments with higher budgets than Astronomy, listing instructors with the number of sections taught, and identifying students who retook courses multiple times. It also includes queries for instructor salaries, department budgets, and average salaries across departments. Additionally, the document provides information on courses taught in specific semesters and the number of instructors in each department.
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/ 21

Part-01

1. Find the names of those departments whose budget is higher than that of Astronomy. List them
in alphabetic order.

SELECT dept_name FROM department WHERE budget >


(SELECT budget FROM department WHERE dept_name = 'Astronomy') ORDER BY
dept_name;

dept_name
Athletics
Biology
Cybernetics
Finance
History
Math
Physics
Psychology

2.Display a list of all instructors, showing each instructor's ID and the number of sections
taught. Make sure to show the number of sections as 0 for instructors who have not taught any
section.

SELECT instructor.ID AS ID,


COALESCE(COUNT(teaches.sec_id), 0) AS number_of_sections
FROM instructor
LEFT JOIN teaches ON instructor.ID = teaches.ID
GROUP BY instructor.ID
ORDER BY number_of_sections ASC, ID ASC;

ID number_of_sections
16807 0
31955 0
35579 0
37687 0
4034 0
50885 0
52647 0
57180 0
58558 0
59795 0
63395 0
64871 0
72553 0
74426 0
78699 0
79653 0
95030 0
96895 0
97302 0
15347 1
25946 1
4233 1
42782 1
48507 1
48570 1

3. For each student who has retaken a course at least twice (i.e., the student has taken the course
at least three times), show the course ID and the student's ID. Please display your results in
order of course ID and do not display duplicate rows.

SELECT course_id, ID
FROM takes
GROUP BY course_id, ID
HAVING COUNT(*) >= 3
ORDER BY course_id ASC, ID ASC;

course_id ID
362 16480
362 16969
362 27236
362 39925
362 39978
362 44881
362 49611
362 5414
362 69581
362 9993
4. Find the names of Biology students who have taken at least 3 Accounting courses.

SELECT s.name
FROM student s
JOIN takes t ON s.ID = t.ID
JOIN course c ON t.course_id = c.course_id
WHERE s.dept_name = 'Biology'
AND c.dept_name = 'Accounting'
GROUP BY s.ID, s.name
HAVING COUNT(DISTINCT c.course_id) >= 3;

name
Michael
Dalton
Shoji
Uchiyama
Kaminsky
Giannoulis

5. Find the sections that had maximum enrollment in Fall 2010.

WITH EnrollmentCounts AS (
SELECT course_id, sec_id, COUNT(ID) AS enrollment_count
FROM takes
WHERE semester = 'Fall' AND year = 2010
GROUP BY course_id, sec_id
)
SELECT course_id, sec_id
FROM EnrollmentCounts
WHERE enrollment_count = (SELECT MAX(enrollment_count) FROM EnrollmentCounts);

SELECT course_id, sec_id


FROM takes
WHERE semester = 'Fall' AND year = 2010
GROUP BY course_id, sec_id
HAVING COUNT(ID) = (
SELECT MAX(enrollment_count)
FROM (
SELECT COUNT(ID) AS enrollment_count
FROM takes
WHERE semester = 'Fall' AND year = 2010
GROUP BY course_id, sec_id
) AS subquery
);
867 2

6. Find student names and the number of law courses taken for students who have taken at least
half of the available law courses. (These courses are named things like Tort Law or Environment-
tal Law).

SELECT S.name, COUNT(*)


FROM student AS S
JOIN takes T ON S.ID = T.ID
WHERE T.course_id in(
SELECT course_id FROM course WHERE title LIKE "%Law%"
)
GROUP BY S.ID
HAVING COUNT(*) >=
( SELECT COUNT(*)/2 FROM course WHERE title LIKE "%Law%");

name COUNT(*)
Nakajima 4
Nikut 4
Hahn- 4
Nanda 4
Schinag 4
7. Find the rank and name of the 10 students who earned the most A grades (A-, A, A+). Use
alphabetical order by name to break ties. Note: the browser SQLite does not support window
functions.

SELECT ROW_NUMBER() OVER(ORDER BY cnt DESC,name ASC) AS rank,name


FROM (
SELECT S.name, Count(*) AS cnt
FROM student S
JOIN takes T ON T.ID = S.ID
WHERE T.grade in('A+','A','A-')
GROUP BY S.ID
) AS P
LIMIT 10;

rank name
1 Neuhold
2 Greene
3 Hons
4 Lepp
5 Lingamp
6 Mandviwall
7 Drig
8 Fabregas
9 Haigh
10 Heilprin
Part -02

1. Find out the ID and salary of the instructors.

SELECT ID,salary FROM instructor;

ID salary
14365 32241.56
15347 72140.88
16807 98333.65
19368 124651.41
22591 59706.49
25946 90891.69
28097 35023.18
28400 84982.92
31955 71351.42
3199 82534.37
3335 80797.83
34175 115469.11
35579 62579.61
36897 43770.36
37687 104563.38
4034 61387.56
41930 50482.03
4233 88791.45
42782 34272.67
43779 79070.08
48507 107978.47
48570 87549.80
50330 108011.81
50885 32570.50
52647 87958.01

2. Find out the ID and salary of the instructor who gets more than $85,000.

SELECT ID,salary FROM instructor


WHERE salary > 85000;

ID salary
16807 98333.65
19368 124651.41
25946 90891.69
34175 115469.11
37687 104563.38
4233 88791.45
48507 107978.47
48570 87549.80
50330 108011.81
52647 87958.01
63287 103146.87
63395 94333.99
6569 105311.38
73623 90038.09
74420 121141.99
74426 106554.73
77346 99382.59
79653 89805.83
90376 117836.50
95709 118143.98
96895 119921.41
99052 93348.83

3. Find out the department names and their budget at the university.

SELECT dept_name,budget FROM department;

dept_name budget
Accounting 441840.92
Astronomy 617253.94
Athletics 734550.70
Biology 647610.55
Civil Eng. 255041.46
Comp. Sci. 106378.69
Cybernetics 794541.46
Elec. Eng. 276527.61
English 611042.66
Finance 866831.75
Geology 406557.93
History 699140.86
Languages 601283.60
Marketing 210627.58
Math 777605.11
Mech. Eng. 520350.65
Physics 942162.76
Pol. Sci. 573745.09
Psychology 848175.04
Statistics 395051.74
4. List out the names of the instructors from Computer Science who have more than $70,000.

SELECT name FROM instructor WHERE dept_name = 'Comp. Sci.' and salary > 70000;

name
Bourrier
Bondi

5. For all instructors in the university who have taught some course, find their names and the
course ID of all courses they taught.

SELECT name,course_id FROM instructor


NATURAL JOIN teaches;

SELECT name,course_id FROM instructor,teaches WHERE instructor.ID = teaches.ID;

name course_id
Lembr 200
Lembr 843
Bawa 457
Wieland 545
Wieland 581
Wieland 591
DAgostino 338
DAgostino 338
DAgostino 352
DAgostino 400
DAgostino 400
DAgostino 482
DAgostino 599
DAgostino 642
DAgostino 663
DAgostino 867
DAgostino 962
DAgostino 972
DAgostino 991
Liley 192
Kean 366
Kean 808
Atanassov 603
Atanassov 604
Gustafsson 169
6.Find the names of all instructors whose salary is greater than at least one instructor in the
Biology department.

SELECT T.name FROM instructor AS T, instructor as S


WHERE T.salary > S.salary AND T.dept_name='Biology';

SELECT name FROM instructor


WHERE salary > some(
SELECT salary FROM
instructor WHERE dept_name = 'Biology'
);

name
Bawa
Yazdi
Wieland
DAgostino
Liley
Atanassov
Moreira
Gustafsson
Bourrier
Bondi
Soisalon-Soininen
Arias
Murata
Tung
Luo
Romero
Lent
Sarkar
Shuming
Bancilhon
Dusserre
Desyl
Jaekel
McKinnon
Mingoz
7. Find the advisor of the student with ID 12345

SELECT * FROM advisor WHERE s_ID = 12345;

8. Find the average salary of all instructors.

SELECT AVG(salary) AS avg_salary FROM instructor;

77600.188200

9. Find the names of all departments whose building name includes the substring ‘Watson’.

SELECT dept_name FROM department WHERE building LIKE '%Watson%';

10. Find the names of instructors with salary amounts between $90,000 and $100,000.

SELECT name FROM instructor WHERE salary BETWEEN 90000 AND 100000;

name
Yazdi
Liley
McKinnon
Sullivan
Mahmoud
Dale

11. Find the instructor names and the courses they taught for all instructors in the Biology
department who have taught some course.

SELECT I.name,T.course_id FROM instructor I, teaches T WHERE I.ID = T.ID AND


I.dept_name = 'Biology';

SELECT name,course_id FROM instructor NATURAL LEFT JOIN teaches


WHERE dept_name = 'Biology';

name course_id
Queiroz 559
Valtchev 415
Valtchev 702
12. Find the courses taught in Fall-2009 semester.
SELECT course_id FROM teaches WHERE
semester = 'Fall' and year=2009;

course_id
960
242
105
334
304
486
237

13. Find the set of all courses taught either in Fall-2009 or in Spring-2010.

SELECT course_id FROM teaches WHERE


(semester = 'Fall' and year=2009) | (semester = 'Spring' AND year = 2010);

(SELECT course_id FROM teaches WHERE


semester = 'Fall' and year=2009) UNION
(SELECT course_id FROM teaches WHERE
semester = 'Spring' AND year = 2010);

course_id
960
242
692
679
105
334
304
443
486
493
735
270
237
14. Find the set of all courses taught in the Fall-2009 as well as in Spring-2010.

SELECT course_id FROM teaches WHERE


(semester = 'Fall' and year=2009) AND (semester = 'Spring' AND year = 2010);

15. Find all courses taught in the Fall-2009 semester but not in the Spring-2010 semester.

SELECT course_id FROM teaches WHERE


semester = 'Fall' and year=2009 AND
course_id NOT IN (SELECT course_id FROM
teaches WHERE
semester = 'Spring' AND year = 2010);

(SELECT course_id
FROM teaches
WHERE semester = 'Fall' AND year = 2009)
EXCEPT
(SELECT course_id
FROM teaches
WHERE semester = 'Spring' AND year = 2010);

course_id
960
242
105
334
304
486
237

16. Find all instructors who appear in the instructor relation with null values for salary.

SELECT * FROM instructor WHERE salary is null;


17. Find the average salary of instructors in the Finance department.

SELECT AVG(salary) FROM instructor WHERE dept_name='Finance';

105311.380000

18. Find the total number of instructors who teach a course in the Spring-2010 semester.

SELECT COUNT(ID)FROM teaches WHERE semester='Fall' AND YEAR = 2010;

19. Find the average salary in each department.

SELECT dept_name, AVG(salary) AS avg_salary


FROM instructor
GROUP BY dept_name;

SELECT dept_name, AVG(salary) AS avg_salary


FROM department
NATURAL JOIN instructor
GROUP BY dept_name;

dept_name avg_salary
Accounting 48716.592500
Astronomy 79070.080000
Athletics 77098.198000
Biology 61287.250000
Comp. Sci. 98133.470000
Cybernetics 96346.567500
Elec. Eng. 74162.740000
English 72089.050000
Finance 105311.380000
Geology 99382.590000
Languages 57421.856667
Marketing 84097.437500
Mech. Eng. 79813.020000
Physics 114576.900000
Pol. Sci. 100053.073333
Psychology 61143.050000
Statistics 67795.441667

20. Find the number of instructors in each department who teach a course in the Spring-2010
semester.
SELECT dept_name, COUNT(ID) FROM department
NATURAL JOIN instructor
GROUP BY dept_name;

dept_name COUNT(ID)
Accounting 4
Astronomy 1
Athletics 5
Biology 2
Comp. Sci. 2
Cybernetics 4
Elec. Eng. 4
English 4
Finance 1
Geology 1
Languages 3
Marketing 4
Mech. Eng. 2
Physics 2
Pol. Sci. 3
Psychology 2
Statistics 6
21. List out the departments where the average salary of the instructors is more than $42,000.

SELECT dept_name,AVG(salary) AS avg_salary FROM instructor


NATURAL JOIN department
GROUP BY dept_name
HAVING avg_salary > 42000;

dept_name avg_salary
Accounting 48716.592500
Astronomy 79070.080000
Athletics 77098.198000
Biology 61287.250000
Comp. Sci. 98133.470000
Cybernetics 96346.567500
Elec. Eng. 74162.740000
English 72089.050000
Finance 105311.380000
Geology 99382.590000
Languages 57421.856667
Marketing 84097.437500
Mech. Eng. 79813.020000
Physics 114576.900000
Pol. Sci. 100053.073333
Psychology 61143.050000
Statistics 67795.441667

22. For each course section offered in 2009, find the average total credits (tot cred) of all students
enrolled in the section, if the section had at least 2 students.

SELECT course_id, sec_id, AVG(tot_cred) FROM takes


NATURAL JOIN student
WHERE year = 2009
GROUP BY sec_id,course_id
HAVING COUNT(ID)>=2;

course_id sec_id AVG(tot_cred)


105 1 68.3578
242 1 64.4576
304 1 64.9023
334 1 62.8806
486 1 64.8980
604 1 65.7233
960 1 66.0847
972 1 65.2607
237 2 65.6656

23. Find all the courses taught in both the Fall-2009 and Spring-2010 semesters.
SELECT course_id FROM takes
WHERE (semester = 'Fall' and year = 2009) and (semester = 'Spring' and year = 2010);

24. Find all the courses taught in the Fall-2009 semester but not in the Spring-2010 semester.
SELECT course_id FROM takes
WHERE semester = "Fall" and year = 2009
AND course_id NOT IN
(SELECT course_id FROM takes
WHERE semester = "Spring" and year = 2010);

course_id
242
334
486
960
105
105
304
237
237
242
237
334
960
105
304
105
486
304
334
960
486
960
105
334
237
25. Select the names of instructors whose names are neither Mozart nor Einstein
SELECT name FROM instructor
WHERE name NOT in ("Morzat","Einstein");

name
Lembr
Bawa
Yazdi
Wieland
DAgostino
Liley
Kean
Atanassov
Moreira
Gustafsson
Bourrier
Bondi
Soisalon-Soininen
Morris
Arias
Murata
Tung
Luo
Vicentino
Romero
Lent
Sarkar
Shuming
Konstantinides
Bancilhon

26. Find the total number of (distinct) students who have taken course sections taught by the
instructor with ID 110011.

SELECT COUNT(DISTINCT T.ID) AS total_students


FROM takes T
JOIN teaches TE ON T.course_id = TE.course_id AND T.sec_id = TE.sec_id AND T.semester =
TE.semester AND T.year = TE.year
WHERE TE.ID = 110011;

0
27. Find the ID and names of all instructors whose salary is greater than at least one instructor in
the History department.
SELECT ID,name FROM instructor
WHERE salary > some(
SELECT salary FROM instructor
WHERE dept_name = "History");

28. Find the names of all instructors that have a salary value greater than that of each instructor
in the Biology department.

SELECT name FROM instructor


WHERE salary > ALL(
SELECT salary FROM instructor
WHERE dept_name = "Biology"
);

name
Yazdi
Wieland
Liley
Atanassov
Gustafsson
Bourrier
Bondi
Arias
Luo
Romero
Lent
Sarkar
Shuming
Bancilhon
Jaekel
McKinnon
Mingoz
Pimenta
Sullivan
Voronina
Kenje
Mahmoud
Levine
Bietzk
Sakurai
29.Find the departments that have the highest average salary.

SELECT dept_name, AVG(salary) AS highest_salary


FROM instructor
GROUP BY dept_name
ORDER BY highest_salary DESC
LIMIT 1;

Physics 114576.900000

30.Find all courses taught in both the Fall 2009 semester and in the Spring-2010 semester.
SELECT course_id FROM teaches
WHERE ( semester="Fall" and year= 2009)
AND ( semester="Spring" and year = 2010);

31.Find all students who have taken all the courses offered in the Biology department.
SELECT T.ID, S.name
FROM takes T
JOIN student S ON T.ID = S.ID
JOIN course C ON T.course_id = C.course_id
WHERE C.dept_name = 'Biology'
GROUP BY T.ID, S.name
HAVING COUNT(DISTINCT T.course_id) = (SELECT COUNT(*) FROM course WHERE
dept_name = 'Biology');

32. Find all courses that were offered at most once in 2009.
SELECT course_id FROM takes
WHERE year = 2009
GROUP BY course_id
HAVING COUNT(*)<=1;
33. Find all courses that were offered at least twice in 2009.
SELECT course_id FROM takes
WHERE year = 2009
GROUP BY course_id
HAVING COUNT(*)>=2;

course_id
105
237
242
304
334
486
604
960
972

34.Find the average instructors&#39; salaries of those departments where the average salary is
greater than $42,000.

SELECT dept_name,AVG(salary) as avg_salary


FROM instructor
GROUP BY dept_name
HAVING avg_salary > 42000;

dept_name avg_salary
Accounting 48716.592500
Astronomy 79070.080000
Athletics 77098.198000
Biology 61287.250000
Comp. Sci. 98133.470000
Cybernetics 96346.567500
Elec. Eng. 74162.740000
English 72089.050000
Finance 105311.380000
Geology 99382.590000
Languages 57421.856667
Marketing 84097.437500
Mech. Eng. 79813.020000
Physics 114576.900000
Pol. Sci. 100053.073333
Psychology 61143.050000
Statistics 67795.441667
35. Find the maximum across all departments of the total salary at each department.

SELECT dept_name, SUM(salary) as max_salary FROM department


NATURAL JOIN instructor
GROUP BY dept_name
ORDER BY max_salary DESC
LIMIT 1;

SELECT dept_name, MAX(total_salary) AS max_total_salary


FROM (
SELECT dept_name, SUM(salary) AS total_salary
FROM instructor
GROUP BY dept_name
) AS dept_salaries;

Statistics 406772.65

36. List all departments along with the number of instructors in each department.

SELECT dept_name,COUNT(ID) as no_of_instructor


FROM instructor GROUP BY dept_name;

dept_name no_of_instructor
Accounting 4
Astronomy 1
Athletics 5
Biology 2
Comp. Sci. 2
Cybernetics 4
Elec. Eng. 4
English 4
Finance 1
Geology 1
Languages 3
Marketing 4
Mech. Eng. 2
Physics 2
Pol. Sci. 3
Psychology 2
Statistics 6

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy