0% found this document useful (0 votes)
26 views6 pages

SQL Queries

The document provides a collection of SQL queries for practice, categorized into sections such as Basic Queries, String Operations, Set Operations, Aggregate Functions, Subqueries, and Join Expressions. Each section contains multiple example queries that demonstrate how to retrieve specific data from a database, including instructor salaries, course offerings, and student enrollments. The queries utilize various SQL operations like SELECT, JOIN, UNION, and AGGREGATE functions to manipulate and analyze data.

Uploaded by

htetnaung.oo
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)
26 views6 pages

SQL Queries

The document provides a collection of SQL queries for practice, categorized into sections such as Basic Queries, String Operations, Set Operations, Aggregate Functions, Subqueries, and Join Expressions. Each section contains multiple example queries that demonstrate how to retrieve specific data from a database, including instructor salaries, course offerings, and student enrollments. The queries utilize various SQL operations like SELECT, JOIN, UNION, and AGGREGATE functions to manipulate and analyze data.

Uploaded by

htetnaung.oo
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

DBMS Supportive Materials UCSYSG Academics

SQL Queries for Practice


A. Basic Queries
1. Find the names of all instructors whose salary is greater than at least one instructor in the
Comp. Sci. department.
select distinct T.name
from instructor as T, instructor as S
where T.salary > S.salary and S.dept_name = 'Comp. Sci.';

2. 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 as instructor_name, course_id
from instructor, teaches
where instructor.ID= teaches.ID;
B. String Operations
1. Find the names of all departments whose building name includes the substring 'Watson'.
select dept_name
from department
where building like '%Watson%';
C. Set Operations
1. Find the set of all courses taught either in Fall 2017 or in Spring 2018, or both.
(select course_id
from section
where semester = 'Fall' and year= 2017)
union
(select course_id
from section
where semester = 'Spring' and year= 2018);

2. Find the set of all courses taught in both the Fall 2017 and Spring 2018.
(select course_id
from section
where semester = 'Fall' and year= 2017)
intersect
(select course_id
from section
where semester = 'Spring' and year= 2018);

3. Find all courses taught in the Fall 2017 semester but not in the Spring 2018 semester.
(select course_id
from section
where semester = 'Fall' and year= 2017)
except
(select course_id
from section

1
DBMS Supportive Materials UCSYSG Academics

where semester = 'Spring' and year= 2018);


D. Aggregate Functions
1. Find the average salary of instructors in the Computer Science department.
select avg (salary)
from instructor
where dept_name = 'Comp. Sci.';

2. Find the total number of instructors who teach a course in the Spring 2018 semester.
select count(distinct (ID))
from teaches
where semester = 'Spring' and year = 2018;

3. Find the average salary in each department.


select dept_name, avg (salary) as avg_salary
from instructor
group by dept_name;

4. Find the average salary of all instructors.


select avg (salary)
from instructor;

5. Find the number of instructors in each department who teach a course in the Spring 2018
semester.
select dept_name, count(distinct (instructor.ID)) as instr_count
from instructor, teaches
where instructor.ID= teaches.ID and semester = 'Spring'
and year = 2018
group by dept_name;

6. For each course section offered in 2017, find the average total credits (tot_cred) of all
students enrolled in the section, if the section has at least 2 students.
select course_id, semester, year, sec_id, avg (tot_cred)
from student, takes
where student.ID= takes.ID and year = 2017
group by course_id, semester, year, sec_id
having count(student.ID)>=2;
E. Subqueries
1. Find all the courses taught in the both the Fall 2017 and Spring 2018 semesters.
select distinct course_id
from section
where semester = 'Fall' and year= 2017 and
course_id in (select course_id
from section
where semester = 'Spring' and year= 2018);

2
DBMS Supportive Materials UCSYSG Academics

2. Find the total number of (distinct) students who have taken course sections taught by the
instructor with_id 110011.
select count(distinct_id)
from takes
where (course_id, sec_id, semester, year) in
(select course_id, sec_id, semester, year,
from teaches
where teaches.ID= '10101');

3. Find the names of all instructors whose salary is greater than at least one instructor in the
Comp. Sci. department.
select name
from instructor
where salary > some (select salary
from instructor
where dept_name = 'Comp. Sci.');

4. Find the departments that have the highest average salary.


select dept_name
from instructor
group by dept_name
having avg (salary) >= all (select avg (salary)
from instructor
group by dept_name);

5. Find all courses taught in both the Fall 2017 semester and in the Spring 2018 semester.
select course_id
from section as S
where semester = 'Fall' and year= 2017 and
exists (select *
from section as T
where semester = 'Spring' and year= 2018 and
S.course_id= T.course_id);

6. Find all students who have taken all courses offered in the Biology department.
select S.ID, S.name
from student as S
where not exists ((select course_id
from course
where dept_name = 'Biology')
except
(select T.course_id
from takes as T
where S.ID = T.ID));

3
DBMS Supportive Materials UCSYSG Academics

7. Find the total number of (distinct) students who have taken course sections taught by the
instructor with_id 110011.
select count (distinct_id)
from takes
where exists (select course_id, sec_id, semester, year
from teaches
where teaches.ID= '10101'
and takes.course_id = teaches.course_id
and takes.sec_id = teaches.sec_id
and takes.semester = teaches.semester
and takes.year = teaches.year
);

8. Find all courses that were offered at most once in 2017.


select T.course_id
from course as T
where unique (select R.course_id
from section as R
where T.course_id= R.course_id and
R.year = 2017);

9. Find all courses that were offered at least twice in 2017.


select T.course_id
from course as T
where not unique (select R.course_id
from section as R
where T.course_id= R.course_id and
R.year = 2017);

10. Find the average instructors’ salaries of those departments where the average salary is greater
than $42,000.
select dept_name, avg_salary
from (select dept_name, avg (salary) as avg salary
from instructor
group by dept_name) select avg(salary) from instructor group by
where avg_salary > 42000; dept_name having avg(salary) > 42000;

11. Find the maximum across all departments of the total of all instructors’ salaries in each
department.
select max (tot_salary)
from (select dept_name, sum(salary)
from instructor
group by dept_name) as dept total (dept_name, tot_salary);

4
DBMS Supportive Materials UCSYSG Academics

12. Print the names of each instructor, along with their salary and the average salary in their
department.
select name, salary, avg salary
from instructor I1, lateral (select avg(salary) as avg salary
from instructor I2
where I2.dept_name= I1.dept_name);

13. Find those departments with the maximum budget.


with max_budget (value) as
(select max(budget)
from department)
select budget
from department, max_budget
where department.budget = max budget.value;

14. Find all departments where the total salary is greater than the average of the total salary at all
departments.
with dept_total (dept_name, value) as
(select dept_name, sum(salary)
from instructor
group by dept_name),
dept_total_avg(value) as
(select avg(value)
from dept_total)
select dept_name
from dept_total, dept_total_avg
where dept_total.value > dept total_avg.value;

15. List all departments along with the number of instructors in each department.
select dept_name, (select count(*)
from instructor
where department.dept_name = instructor.dept_name)
as num_instructors
from department;

16. Find the ratio of the number of instructors who actually teaches any course to that of
available instructors in the departments.
(select count (*) from teaches)/(select count (*) from
instructor);
F. Join Expressions
1. For all students in the university who have taken some course, find their names and the
course ID of all courses they took.
select name, course id
from student natural join takes;

5
DBMS Supportive Materials UCSYSG Academics

2. List the names of students along with the titles of courses that they have taken.
select name, title
from student natural join takes, course
where takes.course_id = course.course_id;

3. Find all students who have not taken a course.


select ID
from student natural left outer join takes
where course_id is null;

4. Display a list of all students in the Comp. Sci. department, along with the course sections, if
any, that they have taken in Spring 2017; all course sections from Spring 2017 must be
displayed, even if no student from the Comp. Sci. department has taken the course section.
select *
from (select *
from student
where dept name = 'Comp. Sci.')
natural full outer join
(select *
from takes
where semester = 'Spring' and year = 2017);

You might also like