Basic SQL
Basic SQL
3. Integrity
4. View definition
4. Transaction control
6. Authorization
2. Data-manipulation language (DML) provides the ability to query information from the database and to insert tuples
into, delete tuples from, and modify tuples in the database.
3. Integrity includes commands for specifying integrity constraints that the data stored in the database must satisfy.
5. Transaction control includes commands for specifying the beginning and ending of transactions.
6. Embedded and dynamic SQL define how SQL statements can be embedded within general-purpose programming
languages
7. Authorization includes commands for specifying access rights to relations and views.
instead.
• varchar(n): A variable-length character string with user-specified maximum length n. The full form,
- number consists of p digits (plus a sign), and d of the p digits are to the right of the decimal point.
create table R(
A1 D1,
A2 D2,
…….
An Dn,
<integrity constraint1>
<integrity constraint n> );
);
• After creating the second table, add the foreign key constraint to the
first table using ALTER TABLE.
08/31/2025 17
• Create one table without its foreign key first.
• Add the foreign key constraint to the first table using ALTER TABLE.
);
08/31/2025 18
Modification to the Database
• insert into course values ('CS−437' , 'DBS ' , 'Comp. Sci .' ,4 ) ;
• or equivalently
alter table B
add constraint fkname
foreign key ( name ) references A( name ) ;
alter table B
drop constraint fkname ;
create table B(
id number primary key,
name varchar(10),
constraint fkname foreign key ( name ) . . . ) ;
Aug 31, 2025 Dept of ICT 26 / 63
Basic Query Structure
• The select clause can contain arithmetic expressions involving the operation, +, -, *, and /,
and operating on constants or attributes of tuples.
• Return a relation that is same as the instructor relation, except that the value of the attribute salary is
multiplied by 1.1
DEPT_NAME
--------------------
Elec. Eng.
12 rows selected.
Aug 31, 2025 Dept. of I&CT 33
• select ID, name, dept name, salary * 1.1 from
instructor;
SQL> select * from instructor;
ID NAME DEPT_NAME
SALARY
----- -------------------- -------------------- ----------
10101 Srinivasan Comp. Sci.
65000
12121 Wu Finance
90000
15151 Mozart Music
40000
22222 Einstein Physics
95000
32343 El Said History
60000
33456 Gold Physics
87000
45565 Katz Comp. Sci.
75000
58583Aug Califieri
31, 2025 History 62000
Dept. of I&CT 34
• select ID, name, dept name, salary * 1.1 from
instructor;
SQL> select * from instructor; SQL> select ID, name, dept_name, salary * 1.1 from
instructor;
ID NAME DEPT_NAME
SALARY ID NAME DEPT_NAME SALARY*1.1
----- -------------------- -------------------- ---------- ----- -------------------- -------------------- ----------
10101 Srinivasan Comp. Sci. 10101 Srinivasan Comp. Sci. 71500
65000 12121 Wu Finance 99000
12121 Wu Finance 15151 Mozart Music 44000
90000 22222 Einstein Physics 104500
15151 Mozart Music 32343 El Said History 66000
40000 33456 Gold Physics 95700
22222 Einstein Physics 45565 Katz Comp. Sci. 82500
95000 58583 Califieri History 68200
32343 El Said History 76543 Singh Finance 88000
60000 76766 Crick Biology 79200
33456 Gold Physics 83821 Brandt Comp. Sci. 101200
87000
45565 Katz Comp. Sci. ID NAME DEPT_NAME SALARY*1.1
75000 ----- -------------------- -------------------- ----------
58583 Califieri History 98345 Kim Elec. Eng. 88000
62000
76543Aug Singh31, 2025 Finance 12 rows
Dept. selected.
of I&CT 35
80000
SELECT Clause
• distinct
• all
•*
Return a relation that is same as the instructor relation, except that the value of the attribute
salary is multiplied by 1.1
ID NAME DEPT_NAME
SALARY
----- -------------------- -------------------- ----------
10101 Srinivasan Comp. Sci. select name
65000
12121 Wu Finance from instructor
90000
15151 Mozart Music
40000 where dept_name = 'Comp. Sci.'
22222 Einstein Physics
95000
32343 El Said History and salary>80000;
60000
33456 Gold Physics
87000
45565 Katz Comp. Sci.
75000
58583 Califieri History
62000
76543Aug Singh31, 2025 Finance Dept. of I&CT 37
80000
Find all instructors in Comp. Sci. dept with salary greater than ₹80000
SQL> select * from instructor; select name
ID NAME SALARY
----- -------------------- ----------
10101 Srinivasan 65000
12121 Wu 90000
Which instructors are fetched from
15151 Mozart 40000
the table?
22222 Einstein 95000
32343 El Said 60000
45565 Katz 75000
76543 Singh 80000
98345 Kim 80000
8 rows selected.
ID NAME SALARY
----- -------------------- ----------
10101 Srinivasan 65000
12121 Wu 90000
15151 Mozart 40000
22222 Einstein 95000
32343 El Said 60000
45565 Katz 75000
76543 Singh 80000
98345 Kim 80000
ID NAME SALARY
----- -------------------- ----------
12121 Wu 90000
22222 Einstein 95000
33456 Gold 87000
83821 Brandt 92000
• The where clause specifies conditions that the result must satisfy
• Comparison results can be combined using the logical connectives and, or, and
not.
84 rows selected.
Aug 31, 2025 Dept. of I&CT 48
SQL> select name, department.dept_name, building from
instructor, department where instructor.dept_name
=department.dept_name;
NAME DEPT_NAME BUILDING
-------------------- -------------------- ---------------
Srinivasan Comp. Sci. Taylor
Wu Finance Painter
Mozart Music Packard
Einstein Physics Watson
El Said History Painter
Gold Physics Watson
Katz Comp. Sci. Taylor
Califieri History Painter
Singh Finance Painter
Crick Biology Watson
Brandt Comp. Sci. Taylor
12 rows selected.
Aug 31, 2025 Dept. of I&CT 49
SQL> select name, dept_name, building from instructor
natural join department;
NAME DEPT_NAME BUILDING
-------------------- -------------------- ---------------
Srinivasan Comp. Sci. Taylor
Wu Finance Painter
Mozart Music Packard
Einstein Physics Watson
El Said History Painter
Gold Physics Watson
Katz Comp. Sci. Taylor
Califieri History Painter
Singh Finance Painter
Crick Biology Watson
Brandt Comp. Sci. Taylor
12 rows selected.
select *
List the names of instructors along with the course ID of the courses that they taught.
For all instructors in the university who have taught some course, find their names and the course ID of
all courses they taught
Find instructor names and course identifiers for instructors in the Computer Science department
Find the course ID, semester, year and title of each course offered by the Comp. Sci. department
Aug 31, 2025 Dept of ICT 55 / 63
Natural Join department(deptname, building, budget)
course(courseid, title, deptname, credits)
instructor(ID, name, deptname, salary)
section(courseid, sectionid, sem, year, building,
roomnumber, timeslotid)
teaches(ID, courseid, secid, semester, year)
List the names of instructors along with the titles of courses that they
teach
List the names of instructors along with the titles of courses that they teach
select name, title
from instructor natural join teaches natural join course;
ID NAME DEPT_NAME
SALARY
----- -------------------- -------------------- ----------
10101 Srinivasan Comp. Sci.
65000
12121 Wu Finance
90000
15151 Mozart Music
40000
22222 Einstein Physics
95000
32343 El Said History
60000
33456 Gold Physics
87000
45565 Katz Comp. Sci.
75000
58583 Califieri History
62000
76543 Singh Finance
Aug 31, 2025 Dept. of I&CT 59
80000
SQL>select name, title from instructor natural join teaches
natural join course;
NAME TITLE
-------------------- --------------------------------------------------
Crick Intro. to Biology
Crick Genetics
Srinivasan Intro. to Computer Science
Katz Intro. to Computer Science
Brandt Game Design
Brandt Game Design
Srinivasan Robotics
Katz Image Processing
Srinivasan Database System Concepts
Kim Intro. to Digital Systems
Wu Investment Banking
NAME TITLE
-------------------- --------------------------------------------------
El Said World History
Mozart Music Video Production
Einstein Physical Principles
14 rows
Augselected.
31, 2025 Dept. of I&CT 60
Natural Join
List the names of instructors along with the titles of courses that they teach
select name, title
from (instructor natural join teaches) join course using (courseid);
NAME TITLE
-------------------- --------------------------------------------------
Wu Investment Banking
El Said World History
Mozart Music Video Production
Einstein Physical Principles
15 rows selected.
Aug 31, 2025 Dept. of I&CT 62
SQL>select name, title from instructor, teaches,course
where instructor.ID=teaches.ID and
teaches.course_id=course.course_id;
NAME TITLE
-------------------- --------------------------------------------------
Crick Intro. to Biology
Crick Genetics
Srinivasan Intro. to Computer Science
Katz Intro. to Computer Science
Brandt Game Design
NAME TITLE
Brandt Game Design
-------------------- --------------------------------------------------
Crick Game Design
Wu Investment Banking
Srinivasan Robotics
El Said World History
Katz Image Processing
Mozart Music Video Production
Srinivasan Database System Concepts
Einstein Physical Principles
Kim Intro. to Digital Systems
15 rows selected.
NAME TITLE
-------------------- --------------------------------------------------
Wu Investment Banking
El Said World History
Mozart Music Video Production
Einstein Physical Principles
15 rows selected.
Aug 31, 2025 Dept. of I&CT 64
Rename Operation – as clause
• The SQL allows renaming relations and attributes using the as clause
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, courseid
from instructor i, teaches t
where i.id = t.id;
• Another usage of rename operation is a case where we wish to compare tuples in the same
relation.
Find the names of all instructors who have a higher salary than at least one instructor in
'Comp. Sci'.
• Also known as Correlation name, correlation variable, tuple variable, table alias
Aug 31, 2025 Dept of ICT 65 / 63
SQL> select i.name as instructor_name from instructor i
where i.dept_name = 'Finance';
NAME
--------------------
Wu
Singh
7 rows selected.
String Operations
NAME
--------------------
Kim
12 rows selected.
Aug 31, 2025 Dept. of I&CT 69
SQL> select name from SQL> select name from instructor
instructor; where name like '%ri%';
NAME NAME
-------------------- --------------------
Srinivasan Srinivasan
Wu Califieri
Mozart Crick
Einstein
El Said
Gold
Katz
Califieri
Singh
Crick percent (%): The %
Brandt
character matches any
NAME
--------------------
substring
Kim
12 rows selected.
Aug 31, 2025 Dept. of I&CT 70
SQL> select name from SQL> select name from instructor
instructor; where name like ‘_ric_';
NAME
--------------------
Srinivasan
Wu
Mozart
Einstein
El Said
Gold
Katz
Califieri
Singh
Crick
Brandt
NAME
--------------------
Kim
12 rows selected.
Aug 31, 2025 Dept. of I&CT 71
SQL> select name from SQL> select name from instructor
instructor; where name like ‘_ric_';
NAME NAME
-------------------- --------------------
Srinivasan Crick
Wu
Mozart
Einstein
El Said
Gold
Katz underscore ( _ ): The _ character
Califieri
Singh matches any character
Crick
Brandt
NAME
--------------------
Kim
12 rows selected.
Aug 31, 2025 Dept. of I&CT 72
String operation
select name
from instructor
where name like ‘%dar%’;
ID NAME DEPT_NAME
SALARY
----- -------------------- -------------------- ---------- The order by clause causes
12121 Wu Finance 90000
10101 Srinivasan Comp. Sci. the tuples in the result of a
65000
76543 Singh Finance 80000 query to appear in sorted
15151 Mozart Music 40000
98345 Kim Elec. Eng. 80000 order.
45565 Katz Comp. Sci. 75000 Specify desc for descending
33456 Gold Physics 87000
32343 El Said History 60000 order or asc for ascending
22222 Einstein Physics 95000
76766 Crick Biology 72000 order, for each attribute.
58583 Califieri History 62000
ID NAME DEPT_NAME
SALARY
----- -------------------- -------------------- ----------
83821
Aug 31, Brandt
2025 Comp. Sci. Dept.92000
of I&CT 79
• List all the instructors who work in Physics department in ascending and
descending order.
NAME
--------------------
Wu
Einstein
Brandt
NAME
--------------------
Wu
Einstein
Brandt
1 Naveen 300
3 Akash 500
5 Geetha 200
• Aggregate functions are functions that take a collection (a set or multiset) of values as input
and return a single value
• SQL offers five built-in aggregate functions
Average: avg
Minimum: min
Maximum: max
Total: sum
Count: count
• Input to sum and avg must be a collection of numbers, but the other operators can operate on
collections of nonnumeric data types, such as strings, as well.
Aug 31, 2025 Dept of ICT 88 / 63
SQL> select * from instructor; SQL> select max(salary) from instructor;
There are circumstances where we would like to apply the aggregate function
not only to a single set of tuples, but also to a group of sets of tuples - group
by clause
group by
clause
• Find the number of instructors in each department who teach a course in the Fall 2009 semester
• Any attribute that is not present in the group by clause must appear only inside an
aggregate function if it appears in the select clause, otherwise the query is treated as
erroneous
/* erroneous query */
select deptname, name, count (distinct id)
from instructor natural join teaches
where semester = ' Spring ' and year = 2020
group by deptname;
Having clause
Find departments where the average salary of the instructors is more than ₹42,000
DEPT_NAME AVG(SALARY)
-------------------- -----------
Comp. Sci. 77333.3333
Finance 85000
Physics 91000
History 61000
Biology 72000
Elec. Eng. 80000
6 rows selected.
Aug 31, 2025 Dept of ICT 101 / 63
Aggregate Functions
• Find departments where the average salary of the instructors is more than ₹42,000
• Any attribute that is present in the having clause without being aggregated
must appear in the group by clause, otherwise the query is treated as
erroneous.
• Order of execution: from, where, group by, having, order by, select
• Union
• Intersect
• Except (Minus)
Find the set of all courses offered in the Fall 2009 semester
select courseid from section where semester = ' Fall ' and year = 2009;
Find the set of all courses offered in the Spring 2009 semester
select courseid from section where semester = ' Spring ' and year = 2009;
COURSE_I
--------
CS-101
CS-347
PHY-101
SQL> select course_id from section where semester = 'Spring' and year = 2009;
COURSE_I
--------
CS-190
CS-190
EE-181
SQL> select course_id from section where semester = 'Spring' and year = 2009 union
select course_id from section where semester = 'Fall' and year = 2009;
COURSE_I
--------
CS-101
CS-347
PHY-101
SQL> select course_id from section where semester = 'Spring' and year = 2009;
COURSE_I
--------
CS-190
CS-190
EE-181
SQL> select course_id from section where semester = 'Spring' and year = 2009 union
select course_id from section where semester = 'Fall' and year = 2009;
COURSE_I
--------
CS-190
EE-181
CS-101
CS-347
Aug 31, 2025 Dept. of I&CT 111
PHY-101
Set Operations
Find the set of all courses offered either in Fall 2023 or in Spring 2022 or both
select courseid
from section
where semester = 'Fall' and year = 2023
union
select courseid
from section
where semester = 'Spring' and year = 2022;
• The union operation automatically eliminates duplicates, unlike the select clause.
• If the duplicates are needed, then union all is to be used.
COURSE_I COURSE_I
-------- --------
CS-101 CS-101
CS-315 CS-347
CS-319 PHY-101
CS-319
FIN-201
HIS-351
MU-199
SQL> select course_id from section where semester = 'Spring' and year = 2010
intersect
select course_id from section where semester = 'Fall' and year = 2009;
COURSE_I
--------
CS-101
COURSE_I COURSE_I
-------- --------
CS-101 CS-101
CS-315 CS-347
CS-319 PHY-101
CS-319
FIN-201
HIS-351
MU-199
SQL> select course_id from section where semester = 'Spring' and year = 2010
minus select course_id from section where semester = 'Fall' and year = 2009;
COURSE_I
--------
CS-315
CS-319
FIN-201
HIS-351
MU-199
Aug 31, 2025 Dept. of I&CT 115
NULL VALUES
SQL> SELECT
2 name,
3 CASE
4 WHEN salary >= 50000 AND bonus >= 5000 THEN 'Eligible’
5 ELSE 'Not Eligible’
6 END AS eligibility_status
7 FROM employees;
NAME ELIGIBILITY_
-------------------------------------------------- ------------
Alice Eligible
Bob Not Eligible
Carol Not Eligible
Dave Not Eligible
• To perform tests for set membership, set comparisons, and set cardinality
• Find students who have not taken any course yet using set
membership function
• Find students who have not taken any course yet using
set membership function
select name from student where id not in (select id
from takes);
SQL> SELECT course_id FROM teaches WHERE semester = 'Fall' AND year =
2009 AND course_id IN (SELECT course_id FROM teaches WHERE semester =
'Spring' AND year = 2010 );
SQL> SELECT course_id FROM teaches WHERE semester = 'Fall' AND year = 2009 AND
course_id
IN (SELECT course_id FROM teaches WHERE semester = 'Spring' AND year = 2010 );
COURSE_I
--------
CS-101
COURSE_I
--------
CS-347
PHY-101 SQL> SELECT course_id FROM teaches WHERE
SQL> SELECT course_id FROM teaches
WHERE semester = 'Fall' AND year = semester = 'Spring' AND year = 2010
2009 ;
COURSE_I
COURSE_I --------
-------- CS-315
CS-101 FIN-201
CS-347 MU-199
PHY-101 HIS-351
CS-101
CS-319
SQL> 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 );
Find the names of instructors whose names are neither "Mozart“ nor
"Einstein".
Find the total number of (distinct) students who have taken course sections
taught by the instructor with ID 110011
Find the total number of (distinct) students who have taken course sections
taught by the instructor with ID 10101
TOTAL_STUDENTS
--------------
6
• > some comparison in the where clause of the outer select is true if the salary value of
the tuple is greater than at least one member of the set of all salary values for instructors
in Finance department.
• SQL also allows < some, <= some, >= some, = some, and <> some comparisons.
0
(5 = some 5 ) = true
0
(5 some 5 ) = true (since 0 5)
(= some) in
However, ( some) not in
Aug 31, 2025 Dept of ICT 135 / 63
SQL> select * from instructor;
SQL> select * from instructor where dept_name
ID NAME DEPT_NAME
='Finance’;
SALARY
----- -------------------- -------------------- ----------
ID NAME DEPT_NAME
10101 Srinivasan Comp. Sci.
SALARY
65000
----- -------------------- -------------------- ----------
12121 Wu Finance
12121 Wu Finance 90000
90000
76543 Singh Finance 80000
15151 Mozart Music
40000 SQL> SELECT name FROM instructor WHERE salary
22222 Einstein Physics > SOME ( SELECT salary FROM instructor
95000 WHERE dept_name = ‘Finance’ );
32343 El Said History
60000
33456 Gold Physics
87000
45565 Katz Comp. Sci.
75000
58583 Califieri History
62000
76543 Singh Finance
80000
Aug 31, 2025 Dept. of I&CT 136
76766 Crick Biology
SQL> select * from instructor; SQL> select * from instructor where dept_name
='Finance’;
ID NAME DEPT_NAME
SALARY ID NAME DEPT_NAME
----- -------------------- -------------------- ---------- SALARY
10101 Srinivasan Comp. Sci. ----- -------------------- -------------------- ----------
65000 12121 Wu Finance 90000
12121 Wu Finance 76543 Singh Finance 80000
90000
SQL> SELECT name FROM instructor WHERE salary
15151 Mozart Music
> SOME ( SELECT salary FROM instructor
40000
WHERE dept_name = ‘Finance’ );
22222 Einstein Physics
95000
NAME
32343 El Said History
--------------------
60000
Einstein
33456 Gold Physics
Brandt
87000
Wu
45565 Katz Comp. Sci.
Gold
75000
58583 Califieri History
62000
76543 Singh Finance
80000
Aug 31, 2025 Dept. of I&CT 137
76766 Crick Biology
SQL> select * from instructor;
SQL> select * from instructor where dept_name
ID NAME DEPT_NAME
='Finance’;
SALARY
----- -------------------- -------------------- ----------
ID NAME DEPT_NAME
10101 Srinivasan Comp. Sci.
SALARY
65000
----- -------------------- -------------------- ----------
12121 Wu Finance
12121 Wu Finance 90000
90000
76543 Singh Finance 80000
15151 Mozart Music
40000 SQL> SELECT name FROM instructor WHERE salary
22222 Einstein Physics > SOME ( SELECT salary FROM instructor
95000 WHERE dept_name = ‘Finance’ );
32343 El Said History
60000 NAME
33456 Gold Physics --------------------
87000 Einstein
45565 Katz Comp. Sci. Brandt
75000 Wu
58583 Califieri History Gold
62000
76543 Singh Finance
80000
Aug 31, 2025 Dept. of I&CT 138
76766 Crick Biology
SQL> select * from instructor; SQL> select * from instructor where dept_name
='Finance’;
ID NAME DEPT_NAME
SALARY ID NAME DEPT_NAME
----- -------------------- -------------------- ---------- SALARY
10101 Srinivasan Comp. Sci. ----- -------------------- -------------------- ----------
65000 12121 Wu Finance 90000
12121 Wu Finance 76543 Singh name FROM
SQL> SELECT Finance instructor WHERE80000
90000 salary < SOME ( SELECT salary FROM instructor
15151 Mozart Music WHERE dept_name = 'Finance' );
40000
22222 Einstein Physics NAME
95000 --------------------
32343 El Said History Mozart
60000 El Said
33456 Gold Physics Califieri
87000 Srinivasan
45565 Katz Comp. Sci. Crick
75000 Katz
58583 Califieri History Singh
62000 Kim
76543 Singh Finance Gold
80000
Aug 31, 2025 9 rows
Dept. selected.
of I&CT 139
76766 Crick Biology
SQL> select * from instructor; SQL> select * from instructor where dept_name
='Finance’;
ID NAME DEPT_NAME
SALARY ID NAME DEPT_NAME
----- -------------------- -------------------- ---------- SALARY
10101 Srinivasan Comp. Sci. ----- -------------------- -------------------- ----------
65000 12121 Wu Finance 90000
12121 Wu Finance 76543 Singh name FROM
SQL> SELECT Finance instructor WHERE80000
90000 salary <> SOME ( SELECT salary FROM instructor
15151 Mozart Music WHERE dept_name = 'Finance' );
40000
22222 Einstein Physics
95000
32343 El Said History
60000
33456 Gold Physics
87000
45565 Katz Comp. Sci.
75000
58583 Califieri History
62000
76543 Singh Finance
80000
Aug 31, 2025 Dept. of I&CT 140
76766 Crick Biology
SQL> select * from instructor; SQL> select * from instructor where dept_name
='Finance’;
ID NAME DEPT_NAME
SALARY ID NAME DEPT_NAME
----- -------------------- -------------------- ---------- SALARY
10101 Srinivasan Comp. Sci. ----- -------------------- -------------------- ----------
65000 12121 Wu Finance 90000
12121 Wu Finance 76543 Singh name FROM
SQL> SELECT Finance instructor WHERE80000
90000 salary <> SOME ( SELECT salary FROM instructor
15151 Mozart Music WHERE dept_name = 'Finance' );
40000
NAME
22222 Einstein Physics --------------------
95000 Srinivasan
32343 El Said History Wu
Mozart
60000 Einstein
33456 Gold Physics El Said
87000 Gold
45565 Katz Comp. Sci. Katz
Califieri
75000 Singh
58583 Califieri History Crick
62000 Brandt
Kim
76543 Singh Finance
80000 12 rows selected.
Aug 31, 2025 Dept. of I&CT 141
76766 Crick Biology
SQL> select * from instructor;
SQL> SELECT name FROM instructor WHERE salary
ID NAME DEPT_NAME <> SOME ( SELECT salary FROM instructor WHERE
SALARY dept_name = 'Biology' );
----- -------------------- -------------------- ----------
10101 Srinivasan Comp. Sci. NAME
65000 --------------------
12121 Wu Finance Srinivasan
90000 Wu
15151 Mozart Music Mozart
40000 Einstein
22222 Einstein Physics El Said
95000 Gold
32343 El Said History Katz
60000 Califieri
33456 Gold Physics Singh
87000 Brandt
45565 Katz Comp. Sci. Kim
75000
58583 Califieri History 11 rows selected.
62000
76543 Singh Finance
80000
Aug 31, 2025 Dept. of I&CT 142
76766 Crick Biology
Nested Subquery – Set Comparison
Find the names of all instructors who have a salary value greater than that of each
0
instructor in the Finance department. (5 < all 5 ) = false
6
6
V <comp> all r t r (V <comp> t) (5 < all 10 ) = true
4
(5 = all 5 ) = false
4
(5 all 6 ) = true (since 5 4 and 5 6)
( all) not in
However, (= all) in
• SQL also allows < all, <= all, >= all, = all, and <> all comparisons
• <>all is identical to not in, whereas = all is not the same as in.
ID NAME DEPT_NAME
SQL> SELECT name FROM instructor WHERE salary
SALARY
>ALL ( SELECT salary FROM instructor WHERE
----- -------------------- -------------------- ----------
dept_name = 'Comp. Sci.' );
10101 Srinivasan Comp. Sci.
65000
NAME
12121 Wu Finance
--------------------
90000
Einstein
15151 Mozart Music
40000
22222 Einstein Physics
95000
32343 El Said History
60000
33456 Gold Physics
87000
45565 Katz Comp. Sci.
75000
58583 Califieri History
62000
76543 Singh Finance
80000
Aug 31, 2025 Dept. of I&CT 146
76766 Crick Biology
Test for Empty Relation
• The exists construct returns the value true if the argument subquery is nonempty
query
Find all courses offered in both the Fall 2009 and in the Spring 2010
select course_id
from section S
where semester = 'Fall' and year = 2009
and exists ( select course_id
from section T
where semester = 'Spring' and year = 2010 and S.course_id =
T.course_id) ;
COURSE_I
--------
CS-101
IN Exists
Scans all rows returned by the sub Terminates the processing of the
query to conclude the result subquery once the sub query returns the
first row
Return all rows where the attribute Returns true of the subquery returns any
value is present in the subquery rows, otherwise, it return false
select *
Exists or Not exists solely check the
from tablename
where id in (subquery); existences of rows in the sub query
select *
from tablename
where id = 1 OR id = 2 OR id = 3 OR
id = null;
Aug 31, 2025 Dept. Of I&CT 155
Test for Empty Relation
Find all students who have taken all courses offered in Finance department
department(dept_name, building, budget)
course(course_id, title, dept_name, credits)
instructor(ID, name, dept_name, salary)
section(course_id, sec_id, semester, year, building,
room_number, time_slot_id)
teaches(ID, course_id, sec_id, semester, year)
student(ID, name, dept_name, tot_cred)
takes(ID, course_id, sec_id, semester, year, grade)
Find all students who have taken all courses offered in Finance department
select id, name from student s where not exists (( select course_id from course where
dept_name = ‘Finance’ ) minus (select course_id from takes t where s.id = t.id));
Find all students who have taken all courses offered in Finance department
SQL> select id, name from student s where not exists (( select
course_id from course where dept_name = 'Finance' ) minus (select
course_id from takes t where s.id = t.id));
ID NAME
----- --------------------
23121 Chavez
Find the average instructor’s salaries of those departments where the average
salary is greater than ₹42000
Aug 31, 2025 Dept. Of I&CT 162
Sub Queries in FROM clause
Find the average instructor’s salaries of those departments where
the average salary is greater than ₹42000
DEPT_NAME AVG_SALARY
-------------------- ----------
Comp. Sci. 77333.3333
Finance 85000
Physics 91000
History 61000
Biology 72000
Elec. Eng. 80000
Aug 31, 2025 Dept. Of I&CT 164
Sub Queries in FROM clause
• Find the average instructor’s salaries of those departments where the average salary is greater than ₹42000
• Find the average instructor’s salaries of those departments where the average salary is
greater than ₹42000
• Find the maximum salary across all departments of the total salary at each department
• Print the names of each instructor, along with their salary and the average salary in the
department
Aug 31, 2025 Dept. Of I&CT 166
Sub Queries in FROM clause
50000)
Find those departments with the maximum budget using with clause
Find all departments where the total salary is greater than the average of the total
salary at all departments using with clause
DEPTNAME VAL
-------------------- ----------
Physics 182000
Comp. Sci. 232000
Finance
Aug 31, 2025 170000 Dept. of I&CT 175
Scalar Sub-Queries
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 numinstructor
from department;
Aug 31, 2025 Dept. Of I&CT 177
Scalar Sub-Queries
• If the result has more than one tuple when the subquery is executed, a run-
time error
Delete all tuples in the instructor relation for those instructors associated with a
department located in the Watson building
Delete the records of all instructors with salary below the average at the university
1 row created.
1 row created.
1 row deleted.
from student
completed by the student. Assume that a course is successfully completed if the student has a
update student S