3 SQL
3 SQL
3 SQL
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>), ….
);
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)
);
CREATE TABLE Course
( CourseNo CHAR(6),
CrsDesc VARCHAR(250),
CrsCredit SMALLINT,
PRIMARY KEY (CourseNo),
UNIQUE (CrsDesc)
);
CREATE TABLE Offering
( OfferNo CHAR(6),
CourseNo CHAR(6),
OffTerm SMALLINT NOT NULL,
OffYear INT NOT NULL,
OffLocation CHAR(6),
OffTime CHAR(8),
OffDays CHAR(10),
FacID CHAR(8)
PRIMARY KEY (OfferNo),
FOREIGN KEY (CouseNo) REFERENCES Course(CourseNo)
Table Enrollment (OfferNo, StdID, EnrGrade)
Table Faculty (FacID, FacName, FacDept, FacSalary, FacSupervisor)
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>;
SELECT Statement
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, CourseNo
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, CourseNo
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.CourseNo, OffDays, OffTime
FROM Offering, Course
WHERE Course.CourseNo = Offering.CourseNo
AND OffTerm = 1 AND OffYear = 2021
AND CrsDesc LIKE ‘%Database%’;
(In Access: CrsDesc LIKE ‘*Database*’)
14. List the student name, offering number and grade of students who have a grade
>= 3.5 in a course offering.
15. List the names of students who have grades >= 3.5 in a course offering.
16. List the names of students (without duplicates) who have grades >= 3.5 in a
course offering.
Joining Tables
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
Retrieve the name, class and grade of students who have greater than or equal 3.5
in a course offered in term 2 year 2020.
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
Retrieve the name, class and grade of students who have a greater than or equal
to 3.5 in a course offered in term 2 year 2020 taught by ‘Nguyen Nam’.
List the offer number, course number, and name of the instructor of MI course
offering scheduled in term 1 year 2021.
Enrollment
OfferNo StdID EnrGrade
11111 20191000 5
11112 20191000 6
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
The MIN() aggregate function returns the lowest value (minimum) in a set
MIN
of non-NULL values.
SELECT count(*)
FROM Student;
SELECT count(*)
FROM Student
WHERE StdGPA >= 3.0;
SELECT count(StdID)
FROM Student
WHERE StdGPA >= 3.0;
SQL Aggregate Functions
SELECT MAX(StdGPA)
FROM Students;
SELECT count(StdID)
FROM (Course INNER JOIN Offering ON Course.CrsNO = Offering.CrsNo) INNER JOIN
Enrollment ON Offering.OfferNo = Enrollment.OfferNo
WHERE CrsDesc LIKE ‘MI*’ AND OffTerm = 1 AND OffYear = 2021;
SELECT count(StdID)
FROM Course, Offering, Enrollment
WHERE Course.CrsNO = Offering.CrsNo AND Offering.OfferNo = Enrollment.OfferNo
AND CrsDesc LIKE ‘MI*’ AND OffTerm = 1 AND OffYear = 2021;
SQL Aggregate Functions
List the GPA, , ID, name of “EM” major students. Order the result by GPA in
ascending order
SELECT StdGPA, StdID, StdName
FROM Student
WHERE StdMajor = ‘EM’
ORDER BY StdGPA;
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 faculty. Order the result by ascending
name and descending salary.
Combining Joins and Grouping:
• List the course number, the offering number and the number of students enrolled.
Only include course offered in term 2 year 2020.
SELECT CourseNo, Enrollment.OfferNo, COUNT (*) AS NumStudent
FROM Offering, Enrollment
WHERE Offering.OfferNo = Enrollment.OfferNo AND OffTerm = 2 AND OffYear = 2020
GROUP BY Enrollment.OfferNo , CourseNo;
List the course number, offer number, and average grade of students enrolled in
the term 2 year 2020 MI course offerings in which more than ten students is
enrolled. Sort the result by course number in ascending order and average grade in
descending order.
SELECT CourseNo, Offering.OfferNo, AVG(EnrGrade) AS AvgGrade
FROM Enrollment, Offering
WHERE Enrollment.OfferNo = Offering.OfferNo AND CourseNo LIKE ‘MI%’ AND OffTerm =
2 AND OffYear = 2020
GROUP BY CourseNo, Offering.OfferNo
HAVING COUNT(*) > 1
ORDER BY CourseNO, AVG(EnrGrade) DESC;
• In the ORDER BY clause, note the number 3 as the second column to sort:
ORDER BY CourseNO, 3 DESC;
Traditional Set Operators in SQL
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