0% found this document useful (0 votes)
16 views73 pages

4.SQL 1

The document provides an overview of SQL (Structured Query Language), detailing its role as the standard language for relational database systems and outlining its components, including Data Definition Language (DDL) and Data Manipulation Language (DML). It also covers the history of SQL, examples of creating tables, modifying schemas, and executing various SQL queries, including joins and aggregate functions. Additionally, it explains the use of SQL aliases and the differences between inner and outer joins.

Uploaded by

Quỳnh Phươnn
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)
16 views73 pages

4.SQL 1

The document provides an overview of SQL (Structured Query Language), detailing its role as the standard language for relational database systems and outlining its components, including Data Definition Language (DDL) and Data Manipulation Language (DML). It also covers the history of SQL, examples of creating tables, modifying schemas, and executing various SQL queries, including joins and aggregate functions. Additionally, it explains the use of SQL aliases and the differences between inner and outer joins.

Uploaded by

Quỳnh Phươnn
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/ 73

Database Systems

DATABASE QUERY WITH SQL


SQL Database Overview

 SQL (Structured Query Language) is the standard language for Relational Database
System.
• It is designed over relational algebra and tuple relational calculus
• All the Relational Database Management Systems (RDMS) like MySQL, MS Access,
Oracle, Sybase, Informix, Postgres, and SQL Server use SQL as their standard database
language.
 SQL is Structured Query Language, which is a computer language for storing,
manipulating, and retrieving data stored in a relational database.
 Data Definition Language (DDL)
• CREATE: It creates a new table, a view of a table.
• ALTER: It modifies the existing table.
• DROP: It deletes the entire table or other objects in the database.
 Data Manipulation Language (DML)
• SELECT: It extracts certain records from one or more tables.
• INSERT: It creates a record (row) in the existing table.
• UPDATE: It modifies the existing record (row) of the table.
• DELETE: It deletes the records (rows) in the table.
 Brief History of SQL
• The SQL programming language was first developed in the 1970s by IBM researchers
Raymond Boyce and Donald Chamberlin. The programming language, known then as
SEQUEL, was created following the publishing of Edgar Frank Todd's paper, "A
Relational Model of Data for Large Shared Data Banks," in 1970.
Year Event
1972 System R project at IBM Research Labs
1974 SQUARE
1975 Language revision and name change to SEQUEL
1976 Language revision and name change to SEQUEL2
1977 Name change to SQL
1978 First commercial implementation by Oracle Corporation
1981 IBM product SQL/DS featuring SQL
1986 SQL-86 (SQL1) standard approved
1989 SQL-89 standard approved (revision to SQL-86)
1992 SQL-92 (SQL2) standard approved
1999 SQL-1999 (SQL3) standard approved
CREATE TABLE statements in standard SQL

 CREATE TABLE
CREATE TABLE <table_name>
(<column1> <datatype> [NOT NULL],
<column2> <datatype> [NOT NULL],
<column3> <datatype> [NOT NULL],.....
<columnN> <datatype> [NOT NULL],
[CONSTRAINT <constraintname>] PRIMARY KEY( <one or more columns>),
[CONSTRAINT <constraintname>] UNIQUE (<one column>),
[CONSTRAINT <constraintname>] FOREIGN KEY (<fk column>)
REFERENCES <tablename>(<column>), ….
);
Example Database

Tables:
 Student (StdID, StdName, StdBirthday, StdMajor, StdClass, StdGPA)
 Course (CrsNo, CrsDesc, CrsCredit)
 Lecturer (LecID, LecName, LecDept, LecSalary, LecSupervisor)
 Offering (OfferNo, CrsNo, OffTerm, OffYear, OffLocation, OffTime, OffDay,LecID)
 Enrollment (OfferNo, StdID, EnrGrade)

CREATE TABLE Student
( StdID CHAR(8),
StdName VARCHAR(50),
StdBirthday DATE,
StdMajor CHAR(6),
StdClass CHAR(30),
StdGPA DECIMAL(3,2),
CONSTRAINT PKStudent PRIMARY KEY (StdID)
);
Or CONSTRAINT PKStudent PRIMARY KEY (StdID)
CREATE TABLE Course
( CrsNo CHAR(8),
CrsDesc VARCHAR(250),
CrsCredit SMALLINT,
PRIMARY KEY (CrsNo),
UNIQUE (CrsDesc)
);
CREATE TABLE Offering
( OfferNo CHAR(8),
CrsNo CHAR(8),
OffTerm SMALLINT NOT NULL,
OffYear INT NOT NULL,
OffLocation CHAR(6),
OffTime CHAR(8),
OffDay CHAR(10),
LecID CHAR(8)
PRIMARY KEY (OfferNo),
FOREIGN KEY (CouseNo) REFERENCES Course(CrsNo)
);
 Table Enrollment (OfferNo, StdID, EnrGrade)
 Table Lecturer (LecID, LecName, LecDept, LecSalary, LecSupervisor)
 Create Table - By Copying all columns from another table
CREATE TABLE <new_table> AS (<SELECT statement>);
Modifying Relation Schema

 Delete a table:
DROP TABLE <tablename>;

 Add Column:
ALTER TABLE <tablename>
ADD <columnname> <datatype>;

 Drop Column:
ALTER TABLE <tablename>
DROP <columnname>;
Database Modifications

 INSERT: insert rows into a table.


 DELETE: delete certain rows from a table
 UPDATE: update values of certain components of certain existing rows.
 INSERT: insert rows into a table.
INSERT INTO <table name> [(list of column names)] VALUES (v1, v2, …., vn);

 INSERT INTO Course VALUES (‘MI3030’, ‘Toan Kinh te’, 3)


 INSERT INTO Course(CrsNo, CrsDesc, CrsCredit) VALUES (‘MI3030’, ‘Toan Kinh te’, 3)
 INSERT INTO Course(CrsDesc, CrsNo, CrsCredit) VALUES (‘Toan Kinh te’, ‘MI3030’, 3)
 Insert data into a table from another table:
 INSERT INTO <table name> [(list of column names)] SELECT …… ;

 CREATE TABLE EMStudent


(StdID CHAR(8),
StdName VARCHAR(50),
StdClass CHAR(30));

 INSERT INTO EMStudent


SELECT StdID, StdName, StdClass FROM Student WHERE StdMajor = ‘EM’;
 DELETE: delete certain rows from a table

DELETE FROM <table name> [WHERE <condition>];

 DELETE FROM EMStudent WHERE SdtID = ‘20201000’;

 DELETE FROM EMStudent;


 UPDATE: update values of certain components of certain existing rows.

UPDATE <table name>


SET <new-value assignments> [ WHERE <condition>];

UPDATE Course SET CrsCredit = 2 WHERE CrsNo = ‘MI2000’;


SELECT Statement

 The SELECT statement is used to select data from a database.

SELECT <list of columns and expressions usually involving columns>


FROM <list of tables and join operations>
WHERE <condition>
GROUP BY <list of grouping columns>
HAVING <condition>
ORDER BY <list of sorting specifications>
Simple SQL statement with a single table

1. SELECT * FROM Student;


(* Show all columns)
2. SELECT StdID, StdName FROM Student;

3. SELECT StdID FROM Enrollment;

4. SELECT DISTINCT StdID FROM Enrollment;


• Using DISTINCT keyword to move duplicates in the result.
5. SELECT StdID, StdName FROM Student WHERE StdGPA >=3.0;
Simple SQL statement with a single table

6. SELECT * FROM Course WHERE CrsNo LIKE ‘MI%’;


7. SELECT * FROM Course WHERE CrsNo LIKE ‘MI200_’;
( %, _ is the wildcard character. In Acess: *, ?)
8. SELECT StdID, StdName, StdClass FROM Student
WHERE StdBirthday BETWEEN ‘1/1/2000’ AND ‘31/12/2000’;
(In Access: # 1/1/2000 # AND #31/12/2000#)

9. SELECT StdID, StdName, StdClass FROM Student


WHERE StdBirthday IS NULL;
10. SELECT StdID, StdName, StdClass FROM Student
WHERE StdBirthday IS NOT NULL;
Simple SQL statement with a single table

11. List the offer number, course number for course offerings scheduled in term 2 in year
2020 or term 1 in year 2021.
SELECT OfferNo, CrsNo
FROM Offering
WHERE (OffTerm = 2 AND OffYear = 2020) OR (OffTerm = 1 AND OffYear = 2021);

12. List the offer number, course number of term 1 year 2021 offerings without
information about location.
SELECT OfferNo, CrsNo
FROM Offering
WHERE OffLocation IS NULL AND (OffTerm = 1 AND OffYear = 2021);
Joining Tables

13. List the offering number, course number, days, and time of offerings containing
the words ‘Database’ in the course description and taught in term 1 year 2021.
SELECT OfferNo, Offering.CrsNo, OffDays, OffTime
FROM Offering, Course
WHERE Course.CrsNo = Offering.CrsNo
AND OffTerm = 1 AND OffYear = 2021
AND CrsDesc LIKE ‘%Database%’;
(In Access: CrsDesc LIKE ‘*Database*’)

SELECT OfferNo, Offering.CrsNo, OffDays, OffTime


FROM Offering INNER JOIN Course ON Course.CrsNo = Offering.CrsNo
WHERE OffTerm = 1 AND OffYear = 2021 AND CrsDesc LIKE ‘%Database%’;
Joining Tables

14. List the ID of students who have a grade >= 3.5 in a course offering.
15. List the ID of students (without duplicates) who have grades >= 3.5 in a course
offering.

16. List the ID, names of students who have grades >= 3.5 in a course offering.
Joining Tables

17. List the student ID, names and the offering number in which the grade is greater
than 3.0 and the offering is given in term 2 year 2020.
SELECT Student. StdID, StdName, Enrollment.OfferNo
FROM Student, Enrollment, Offering
WHERE Student.StdID = Enrollment.StdID
AND Offering.OfferNo = Enrollment.OfferNo
AND OffYear = 2020 AND OffTerm = 2
AND EnrGrade >= 3.0;
Joining Tables

Combine the Cross Product and Join Operator Styles

SELECT Student. StdID, StdName, Enrollment.OfferNo


FROM Student INNER JOIN Enrollment ON Student.StdID = Enrollment.StdID, Offering
WHERE Offering.OfferNo = Enrollment.OfferNo
AND OffYear = 2020 AND OffTerm = 2
AND EnrGrade >= 3.0;
Joining Tables

18. Retrieve the name, class and grade of Student who have greater than or equal 3.5
in a course offered in term 2 year 2020.

SELECT StdName, StdClass, EnrGrade


FROM (Student INNER JOIN Enrollment
ON Student.StdID = Enrollment.StdID)
INNER JOIN Offering ON Offering.OfferNo = Enrollment.OfferNo
WHERE EnrGrade >= 3.5 AND OffTerm = 2 AND OffYear = 2020;
Joining Tables
Self Join
19. List lecturer member who have a higher salary than their supervisor. List the ID,
name and salary of the lecturer and supervisor.
SELECT T.LecID, T.LecName, T.LecSalary, S.LecID,
S.LecName, S.LecSalary
FROM Lecturer AS T, Lecturer AS S
WHERE T.LecSupervisor = S.LecID
AND T.LecSalary > S.LecSalary;

 (T, S are Alias Table Names)


.
Joining Tables

 SQL Aliases
• SQL aliases are used to give a table, or a column in a table, a temporary name.
• Aliases are often used to make column names more readable.
• An alias only exists for the duration of that query.
• An alias is created with the AS keyword

 Alias Column Syntax


SELECT column_name [AS] alias_name
FROM table_name;

 Alias Table Syntax


SELECT column_name(s)
FROM table_name [AS] alias_name;
Joining Tables

SELECT S.StdID, StdName, Enrollment.OfferNo


FROM Student AS S, Enrollment AS E, Offering AS O
WHERE S.StdID = E.StdID
AND O.OfferNo = E.OfferNo
AND OffYear = 2020 AND OffTerm = 2
AND EnrGrade >= 3.0;
Joining Tables

20. Retrieve the name, class and grade of Student who have a greater than or equal
to 3.5 in a course offered in term 2 year 2020 taught by ‘Nguyen Nam’.

21. List the offer number, course number, and name of the instructor of MI course
offering scheduled in term 1 year 2021. (CrsNo LIKE ‘MI*’)
OUTER JOIN
Student
StdID StdName StdBirthday StdMajor StdClass StdGPA
20191000 Hoa 12/4/2001 EM Kinh tế 2.50
20191001 Cúc 1/1/2000 MI Toán Tin 3.00

Enrollment
OfferNo StdID EnrGrade
11111 20191000 5
11112 20191000 6

 SELECT Student.StdID, Student.StdName, Enrollment.OfferNo,


Enrollment.EnrGrade StdID StdName OfferNo EnrGrade

FROM Student INNER JOIN Enrollment ON Student.StdID = Enrollment.StdID; 20191000 Hoa 11111 5

20191000 Hoa 11112 6

 SELECT Student.StdID, Student.StdName, Enrollment.OfferNo,


Enrollment.EnrGrade StdID StdName OfferNo EnrGrade
20191000 Hoa 11111 5
FROM Student LEFT OUTER JOIN Enrollment ON Student.StdID = 20191000 Hoa 11112 6

Enrollment.StdID; 20191001 Cúc


OUTER JOIN

 INNER join is used to retrieve rows from two or more tables by matching a field
value that is common between the tables. The fields you join on must have similar
data type (you cannot join on MEMO or OLEOBJECT data types).

 An OUTER JOIN is used to retrieve rows from multiple tables while preserving rows
from one of the tables, even if there is no matching row in the other table. There
are two types of OUTER JOINs: LEFT OUTER JOIN and RIGHT OUTER JOIN.
SQL Aggregate Functions

Aggregate function Description

The AVG() aggregate function calculates the average of non-NULL values


AVG
in a set.

The COUNT() aggregate function returns the number of rows in a group,


COUNT
including rows with NULL values.

The MAX() aggregate function returns the highest value (maximum) in a


MAX
set of non-NULL values.

The MIN() aggregate function returns the lowest value (minimum) in a set
MIN
of non-NULL values.

The SUM() aggregate function returns the summation of all non-NULL


SUM
values a set.
SQL Aggregate Functions

SELECT count(*)
FROM Student;
StdID StdName StdBirthday StdMajor StdClass StdGPA

20201000 Nguyen Hoa 11/01/2001 EM Economics 1 3.5

20201001 Le Van Nam 22/2/2002 MI Math1 2.2

20201002 Tran Van Trung 5/5/2002 CS Computer Science 1 3.1


20203000 Hoang Mai 6/6/2000 MI Math2 2.9

20202000 Le Van 7/7/2002 EM Economics 2 3.0

20202001 Vu Hai 8/8/2001 MI Math1 3.2


SELECT count(*) FROM Student WHERE StdGPA >= 3.0;
SELECT count(StdID) FROM Student WHERE StdGPA >= 3.0;
StdID StdName StdBirthday StdMajor StdClass StdGPA

20201000 Nguyen Hoa 11/01/2001 EM Economics 1 3.5

20201001 Le Van Nam 22/2/2002 MI Math1 2.2

20201002 Tran Van Trung 5/5/2002 CS Computer Science 1 3.1


20203000 Hoang Mai 6/6/2000 MI Math2 2.9

20202000 Le Van 7/7/2002 EM Economics 2 3.0

20202001 Vu Hai 8/8/2001 MI Math1 3.2


SQL Aggregate Functions

SELECT MAX(StdGPA)
FROM Student;
StdID StdName StdBirthday StdMajor StdClass StdGPA

20201000 Nguyen Hoa 11/01/2001 EM Economics 1 3.5

20201001 Le Van Nam 22/2/2002 MI Math1 2.2

20201002 Tran Van Trung 5/5/2002 CS Computer Science 1 3.1


20203000 Hoang Mai 6/6/2000 MI Math2 2.9

20202000 Le Van 7/7/2002 EM Economics 2 3.0

20202001 Vu Hai 8/8/2001 MI Math1 3.2


SELECT COUNT(*), AVG(StdGPA)
FROM Student
WHERE StdMajor = ‘EM’;
StdID StdName StdBirthday StdMajor StdClass StdGPA

20201000 Nguyen Hoa 11/01/2001 EM Economics 1 3.5

20201001 Le Van Nam 22/2/2002 MI Math1 2.2

20201002 Tran Van Trung 5/5/2002 CS Computer Science 1 3.1


20203000 Hoang Mai 6/6/2000 MI Math2 2.9

20202000 Le Van 7/7/2002 EM Economics 2 3.0

20202001 Vu Hai 8/8/2001 MI Math1 3.2


 SELECT COUNT(StdID) FROM Enrollment WHERE EnrGrade < 4;
 SELECT COUNT(DISTINCT StdID) FROM Enrollment WHERE EnrGrade < 4;
OfferNo StdID EnrGrade
11111 20201000 1
11111 20201001 7
11111 20201002 3
11113 20201001 5
11113 20201002 2
SQL Aggregate Functions

SELECT count( DISTINCT StdID)


FROM (Course INNER JOIN Offering ON Course.CrsNo = Offering.CrsNo) INNER JOIN
Enrollment ON Offering.OfferNo = Enrollment.OfferNo
WHERE CrsDesc LIKE ‘*Database*’ AND OffTerm = 1 AND OffYear = 2021;

SELECT count(DISTINCT StdID)


FROM Course, Offering, Enrollment
WHERE Course.CrsNo = Offering.CrsNo AND Offering.OfferNo = Enrollment.OfferNo
AND CrsDesc LIKE ‘*Database’ AND OffTerm = 1 AND OffYear = 2021;
SQL Aggregate Functions

SELECT sum(CrsCredit * EnrGrade)


FROM Course, Offering, Enrollment
WHERE Course.CrsNo = Offering.CrsNo AND Offering.OfferNo = Enrollment.OfferNo
AND StdID = "20191000" AND OffTerm = 1 AND OffYear = 2021;
SELECT sum(CrsCredit * EnrGrade)/sum(CrsCredit)
FROM Course, Offering, Enrollment
WHERE Course.CrsNo = Offering.CrsNo AND Offering.OfferNo = Enrollment.OfferNo
AND StdID = ‘20191000’ AND OffTerm = 1 AND OffYear = 2021;
Summarizing Tables with GROUP BY and HAVING

 Summarize the average GPA of Student by major


SELECT StdMajor, AVG(StdGPA) FROM Student
GROUP BY StdMajor;
StdID StdName StdBirthday StdMajor StdClass StdGPA

20201000 Nguyen Hoa 11/01/2001 EM Economics 1 3.5

20201001 Le Van Nam 22/2/2002 MI Math1 2.2

20201002 Tran Van Trung 5/5/2002 CS Computer Science 1 3.1


20203000 Hoang Mai 6/6/2000 MI Math2 2.9

20202000 Le Van 7/7/2002 EM Economics 2 3.0

20202001 Vu Hai 8/8/2001 MI Math1 3.2


SELECT StdMajor, AVG(StdGPA) AS AvgGPA FROM Student
GROUP BY StdMajor;
COUNT function usage:
• COUNT(*) and COUNT(Column) produce identical results except when “column”
contains NULL values.
• COUNT, AVG, SUM functions support the DISTINCT keyword to restrict the
computation to unique column values
• (Some DBMS do not support the DISTINCT keyword inside of aggregate functions)
 Summarize the number of offerings and unique courses by year.
SELECT OffYear, COUNT(*) AS NumOfferings, COUNT (DISTINCT CrsNo) AS
NumCourses FROM Offering GROUP BY OffYear;
 SELECT StdMajor, AVG(StdGPA) AS AvgGPA
FROM Student
GROUP By StdMajor;

 Summarize the average GPA of Student by major. Only list the major with average
GPA greater than 3.0
SELECT StdMajor, AVG(StdGPA) AS AvgGPA
FROM Student
GROUP By StdMajor
HAVING AVG(StdGPA) > 3.0;
 Summarize the minimum and maximum GPA of Student by major and class
SELECT StdMajor, StdClass, MIN(StdGPA), MAX(StdGPA)
FROM Student
GROUP BY StdMajor, StdClass;

 Summarize the number of MI1111 course offerings by year.


 Summarize the number of MI1111 course offerings by term of each year. Only list
the term has less than 5 of MI1111 course offerings.
 Summarize the number of MI course offerings by course description
 Summarize the number of MI course offerings by course description

SELECT CrsDesc, COUNT(*) AS OfferCount


FROM Course, Offering
WHERE Course.CrsNo = Offering.CrsNo AND Course.CrsNo LIKE ‘MI%’
GROUP BY CrsDesc;
 Combining Joins and Grouping:
• List the course number, the offering number and the number of Student enrolled. Only
include course offered in term 2 year 2020.
 SELECT CrsNo, Enrollment.OfferNo, COUNT (*) AS NumStudent
FROM Offering, Enrollment
WHERE Offering.OfferNo = Enrollment.OfferNo AND OffTerm = 2 AND OffYear = 2020
GROUP BY Enrollment.OfferNo , CrsNo;
Sorting using ORDER BY clause

 Use the ORDER BY clause to sort the result table on one or more columns.

 List the GPA, , ID, name of “EM” major Student. Order the result by GPA in
ascending order
SELECT StdGPA, StdID, StdName
FROM Student
WHERE StdMajor = ‘EM’
ORDER BY StdGPA ASC;
 Order by GPA in descending, student name in ascending order
SELECT StdGPA, StdID, StdName
FROM Student
WHERE StdMajor = ‘EM’
ORDER BY StdGPA DESC, StdName;

 List the salary, name, and department of lecturers. Order the result by ascending
name and descending salary.
 List the course number, offer number, and average grade of Student enrolled in the
term 2 year 2020 MI course offerings in which more than ten Student is enrolled.
Sort the result by course number in ascending order and average grade in
descending order.
 SELECT CrsNo, Offering.OfferNo, AVG(EnrGrade) AS AvgGrade, COUNT(*)
FROM Enrollment, Offering
WHERE Enrollment.OfferNo = Offering.OfferNo AND CrsNo LIKE ‘MI%’ AND OffTerm = 2
AND OffYear = 2020
GROUP BY CrsNo, Offering.OfferNo
HAVING COUNT(*) > 10
ORDER BY CrsNo, AVG(EnrGrade) DESC;

• In the ORDER BY clause, note the number 3 as the second column to sort:
ORDER BY CrsNo, 3 DESC;
Correlated Subquery

 A subquery is best defined as a query within a query. Subqueries enable you to


write queries that select data rows for criteria that are actually developed while
the query is executing at run time. More formally, it is the use of a SELECT
statement inside one of the clauses of another SELECT statement. In fact, a
subquery can be contained inside another subquery, which is inside another
subquery, and so forth. A subquery can also be nested inside INSERT, UPDATE, and
DELETE statements. Subqueries must be enclosed within parentheses.
 A subquery can be used any place where an expression is allowed providing it
returns a single value. This means that a subquery that returns a single value can
also be listed as an object in a FROM clause listing. This is termed an inline view
because when a subquery is used as part of a FROM clause, it is treated like a
virtual table or view. Subquery can be placed either in FROM clause, WHERE clause
or HAVING clause of the main query.
SELECT OfferNo, CrsNo, OffDay, OffTime FROM Offering
WHERE OffTerm = 1 AND OffYear = 2022
AND CrsNo IN (SELECT CrsNo
FROM Course WHERE CrsDesc LIKE ‘%Database%’);
CrsNo CrsDesc CrsCredit
MI2000 Introduction to MI 2
MI3090 Database Systems 3
EM2000 Introduction to BA 2
MI4343 Advanced Database Systems 3

OfferNo CrsNo OffTerm OffYear OffLocation OffTimeS OffTimeE OffDay LecID


11111 MI3090 1 2022 D9-202 4 6 2 M1000
11112 MI3090 2 2022 D5-301 10 12 5 M1000
11113 MI2000 1 2022 D3-202 3 6 4 M1001
11114 MI4343 1 2022 D5-301 9 12 6 M1000
 SELECT OfferNo, CrsNo, OffDays, OffTime
FROM Offering
WHERE OffTerm = 1 AND OffYear = 2022
AND CrsNo = ANY (SELECT CrsNo
FROM Course WHERE CrsDesc LIKE ‘%Database%’);
 Sub query which returns single row output
SELECT OfferNo, CrsNo, OffDay, OffTime
FROM Offering
WHERE OffTerm = 1 AND OffYear = 2022
AND CrsNo = (SELECT CrsNo
FROM Course WHERE CrsDesc = ‘Database Systems’;
 IN (Subquery)
 NOT IN (Subquery)
  ALL (Subquery)
  ANY (Subquery)
  (Single Row Subquery)
 EXISTS (Subquery)
 NOT EXISTS (Subquery)

  is a relational operator: =, ! = , >, >=, <, <=


SELECT CourseDesc FROM Course
WHERE CrsNo NOT IN (SELECT CrsNo
FROM Offering WHERE OffTerm = 2 AND OffYear = 2022
CrsNo CrsDesc CrsCredit
MI2000 Introduction to MI 2
MI3090 Database Systems 3
EM2000 Introduction to BA 2
MI4343 Advanced Database Systems 3
OfferNo CrsNo OffTerm OffYear OffLocation OffTimeS OffTimeE OffDay LecID
11111 MI3090 1 2022 D9-202 4 6 2 M1000
11112 MI3090 2 2022 D5-301 10 12 5 M1000
11113 MI2000 1 2022 D3-202 3 6 4 M1001
11114 MI4343 1 2022 D5-301 9 12 6 M1000
SELECT CourseDesc FROM Course
WHERE EXISTS (SELECT CrsNo FROM Offering
WHERE CrsNo = Course.CrsNo);
CrsNo CrsDesc CrsCredit
MI2000 Introduction to MI 2
MI3090 Database Systems 3
EM2000 Introduction to BA 2
MI4343 Advanced Database Systems 3

OfferNo CrsNo OffTerm OffYear OffLocation OffTimeS OffTimeE OffDay LecID


11111 MI3090 1 2022 D9-202 4 6 2 M1000
11112 MI3090 2 2022 D5-301 10 12 5 M1000
11113 MI2000 1 2022 D3-202 3 6 4 M1001
11114 MI4343 1 2022 D5-301 9 12 6 M1000
SELECT CourseDesc FROM Course
WHERE EXISTS (SELECT CrsNo FROM Offering
WHERE OffTerm = 2 AND OffYear = 2022 AND CrsNo = Course.CrsNo);
CrsNo CrsDesc CrsCredit
MI2000 Introduction to MI 2
MI3090 Database Systems 3
EM2000 Introduction to BA 2
MI4343 Advanced Database Systems 3

OfferNo CrsNo OffTerm OffYear OffLocation OffTimeS OffTimeE OffDay LecID


11111 MI3090 1 2022 D9-202 4 6 2 M1000
11112 MI3090 2 2022 D5-301 10 12 5 M1000
11113 MI2000 1 2022 D3-202 3 6 4 M1001
11114 MI4343 1 2022 D5-301 9 12 6 M1000
SELECT CourseDesc FROM Course
WHERE NOT EXISTS (SELECT * FROM Offering
WHERE OffTerm = 2 AND OffYear = 2022 AND CrsNo = Course.CrsNo);
CrsNo CrsDesc CrsCredit
MI2000 Introduction to MI 2
MI3090 Database Systems 3
EM2000 Introduction to BA 2
MI4343 Advanced Database Systems 3

OfferNo CrsNo OffTerm OffYear OffLocation OffTimeS OffTimeE OffDay LecID


11111 MI3090 1 2022 D9-202 4 6 2 M1000
11112 MI3090 2 2022 D5-301 10 12 5 M1000
11113 MI2000 1 2022 D3-202 3 6 4 M1001
11114 MI4343 1 2022 D5-301 9 12 6 M1000
 Types of Subqueries
• Single Row Sub Query: Sub query which returns single row output. They mark the
usage of single row comparison operators, when used in WHERE conditions.
• Multiple row sub query: Sub query returning multiple row output. They make use of
multiple row comparison operators like IN, ANY, ALL. There can be sub queries
returning multiple columns also.
• Correlated Sub Query: Correlated subqueries depend on data provided by the outer
query. This type of subquery also includes subqueries that use the EXISTS operator to
test the existence of data rows satisfying specified criteria.
SELECT LecID, LecName, LecSalary, LecDept
FROM Lecturer
WHERE LecSalary = (SELECT MIN (LecSalary) FROM Lecturer );

LecID LecName LecDept LecSalary LecSupervisor

M1000 Tuan Mathematics and Informatics 1200 M1001

M1001 Mai Mathematics and Informatics 1000

E2002 Hong Economics and Management 1200


E2003 Trung Economics and Management 1800
SELECT LecDept, MIN (LecSalary)
FROM Lecturer
GROUP BY LecDept
HAVING MIN (LecSalary) < (SELECT AVG (LecSalary) FROM Lecturer) ;

LecID LecName LecDept LecSalary LecSupervisor

M1000 Tuan Mathematics and Informatics 1200 M1001

M1001 Mai Mathematics and Informatics 1000

E2002 Hong Economics and Management 1200


E2003 Trung Economics and Management 1800
Correlated Sub Query

SELECT LecID, LecName, LecSalary, LecDept


FROM Lecturer F
WHERE LecSalary > (SELECT AVG(LecSalary)
FROM Lecturer T
WHERE F. LecDept = T. LecDept);
LecID LecName LecDept LecSalary LecSupervisor

M1000 Tuan Mathematics and Informatics 1200 M1001

M1001 Mai Mathematics and Informatics 1000

E2002 Hong Economics and Management 1200


E2003 Trung Economics and Management 1800
Traditional Set Operators in SQL

 UNION, INTERSECT, MINUS (or EXCEPT)


 Show all MI or EM major Student.
 (SELECT * FROM Student WHERE StdMajor = ‘MI’)
UNION
(SELECT * FROM Student WHERE StdMajor = ‘EM’);
 (SELECT CrsNo FROM Offering WHERE OffTerm = 1 AND OffYear = 2020)
UNION
(SELECT CrsNo FROM Offering WHERE OffTerm = 2 AND OffYear = 2020);
 (SELECT CrsNo FROM Offering WHERE OffTerm = 1 AND OffYear = 2020)
UNION ALL
(SELECT CrsNo FROM Offering WHERE OffTerm = 2 AND OffYear = 2020);
 (SELECT CrsNo FROM Offering WHERE OffTerm = 1 AND OffYear = 2020)
INTERSECT
(SELECT CrsNo FROM Offering WHERE OffTerm = 2 AND OffYear = 2020);
 (SELECT CrsNo FROM Offering WHERE OffTerm = 1 AND OffYear = 2020)
MINUS
(SELECT CrsNo FROM Offering WHERE OffTerm = 2 AND OffYear = 2020);
Indexes

 An index on a column A of a table is a data structure that makes it efficient to find


those rows that have a fixed value for column A. (When tables are very large, it
becomes expensive to scan all the rows of a table to find those rows that match a
given condition.
 CREATE INDEX <index name> ON <table name> (<list of column names>);
 DROP INDEX <index name>;

 CREATE INDEX StdIDX ON Student(StdName);


VIEW Definitions

 CREATE VIEW <view name> AS <view definition >;

 <view definition> is a SELECT statement.

 Table that are defined with a CREATE TABLE statement actually exist in the
database.
 “Tables” are defined with a CREATE VIEW (called VIEWs) do not exist physically.
 Views can be queried as if they existed physically, and in some cases, we can even
modify views.
REFERENCES

1. Hector Garcia-Molina, Jeffrey D. Ullman, Jennifer Widom, DATABASE SYSTEMS:


The Complete Book
2. Ramez Elmasri, Shamkant B. N avathe, FUNDAMENTALS OF
FourthEdition DATABASE SYSTEMS
3. Michael V. Manning, Database Design, Application Development and
Administration.
4. https://www.tutorialspoint.com
5. https://beginnersbook.com
6. https://www.datacamp.com
7. https://www.w3schools.com
8. https://www.sqlservertutorial.net

You might also like