4.SQL 1
4.SQL 1
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
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*’)
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
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.
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
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
FROM Student INNER JOIN Enrollment ON Student.StdID = Enrollment.StdID; 20191000 Hoa 11111 5
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;
StdID StdName StdBirthday StdMajor StdClass StdGPA
SELECT MAX(StdGPA)
FROM Student;
StdID StdName StdBirthday StdMajor StdClass StdGPA
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;
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
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