Chapter 6 - Simple Queries in SQL
Chapter 6 - Simple Queries in SQL
Chapter 6
Simple Queries in SQL
Objectives
1 Bag concept
2 Projection in SQL
3 Selection in SQL
6 NULL values
Projection
Selection
Product
Joins
6.3 SQL Overview
1992 SQL-92 SQL2, FIPS 127-2 Major revision (ISO 9075), Entry Level SQL-92 adopted as FIPS 127-2.
1999 SQL:1999 SQL3 Added regular expression matching, recursive queries, triggers, support for procedural
and control-of-flow statements, non-scalar types, and some object-oriented features.
2003 SQL:2003 Introduced XML-related features, window functions, standardized sequences, and
columns with auto-generated values (including identity-columns).
2006 SQL:2006 ISO/IEC 9075-14:2006 defines ways in which SQL can be used in conjunction with
XML. It defines ways of importing and storing XML data in an SQL database,
manipulating it within the database and publishing both XML and conventional SQL-
data in XML form. In addition, it provides facilities that permit applications to integrate
into their SQL code the use of XQuery, the XML Query Language published by the
World Wide Web Consortium (W3C), to concurrently access ordinary SQL-data and
XML documents.
2008 SQL:2008 Defines more flexible windowing functions, clarifies SQL 2003 items that were still
unclear [1]
6.3 Transact SQL (T-SQL)
T-SQL
SQL
6.3 Sub-languages of T-SQL
T - SQL
DDL
(Data Definition Language)
DML
(Data Manipulation Language)
DCL
(Data Control Language)
6.3 Sub-languages of T-SQL
6.4 SELECT commands
A SELECT statement retrieves information from the database. Using a SELECT
statement, you can do the following:
• Projection: You can use the projection capability in SQL to choose the
columns in a table that you want returned by your query.
• Selection: You can use the selection capability in SQL to choose the rows
in a table that you want returned by a query (with WHERE clause)
• Joining: You can use the join capability in SQL to bring together data that
is stored in different tables by creating a link between them.
6.4 SELECT commands
FROM employees
If you use the ORDER BY clause, it must be the last clause of the SQL
statement.
Expression: Specifies a column on which to sort.
A sort column can be specified as a name or column alias (which can be
qualified by the table or view name), an expression, or a nonnegative
integer representing the position of the name, alias, or expression in
select list.
Multiple sort columns can be specified. The sequence of the sort
columns in the ORDER BY clause defines the organization of the sorted
result set.
Example: Ordering output
Exercise 1
Chapter 6
Queries involving more than one relation
Objectives
2 Dis-ambiguating Attributes
3 Tuple Variables
4 Union
5 Intersection
6 Difference
Review: Capabilities of
SQL SELECT Statements
Projection: You can use the projection capability in SQL to choose the
columns in a table that you want returned by your query.
Selection: You can use the selection capability in SQL to choose the rows
in a table that you want returned by a query (with WHERE clause)
Joining: You can use the join capability in SQL to bring together data that
is stored in different tables by creating a link between them.
Review: A Cartesian Products example
1 – Products & Joins in SQL
δA.class_id = B.class_id(A x B)
SELECT *
FROM student A, class B
WHERE A.class_id = B.class_id
Inner join with non-equal condition
If a row does not satisfy a join condition, the row will not appear in the
query result. These rows are called dangling rows (or dangling tuples).
CLASS STUDENT
CLASS_ID CLASS_NAME CLASS_ID ID NAME
106 Lop 106 106 1A
107 Lop 107 106 2B
201 Lop 201
107 3C
202 Lop 202
107 4D
5E
6F
7G
8H
Left Outer Join
CLASS STUDENT
CLASS_ID CLASS_NAME CLASS_ID ID NAME
106 Lop 106 106 1A
107 Lop 107 106 2B
201 Lop 201 107 3C
202 SELECT A.class_name
Lop 202 107 4D
5E
FROM class A 6F
7G
LEFT OUTER JOIN student B
8H
ON A.class_id = B.class_id
Output of LEFT OUTER JOIN
CLASS STUDENT
CLASS_ID CLASS_NAME CLASS_ID ID NAME
106 Lop 106 106 1A
106 Lop 106 106 2B
107 Lop 107 107 3C
107 Lop 107 107 4D
201 Lop 201
202 Lop 202
ON A.class_id = B.class_id
Output of RIGHT OUTER JOIN
CLASS STUDENT
CLASS_IDCLASS_NAME CLASS_ID ID NAME
106 Lop 106 106 1 A
106 Lop 106 106 2 B
107 Lop 107 107 3 C
107 Lop 107 107 4 D
5 E
6 F
7 G
8 H
CLASS STUDENT
CLASS_IDCLASS_NAME CLASS_ID ID NAME
106 Lop 106 106 1 A
106 Lop 106 106 2 B
107 Lop 107 107 3 C
107 Lop 107 107 4 D
5 E
6 F
7 G
8 H
201 Lop 201
202 Lop 202
19
Cartesian Products
Two relations:
MovieStar (name, address, gender, birthdate)
MovieExec (name, address, cert#, netWorth)
Both relations have attributes “name” and “address”. Look
at the following query to see the way of dis-ambiguating:
{A ∩ B} = {a | a is in A and B}
Difference
SQL Server
Oracle
ĐẠI HỌC FPT CẦN THƠ
Chapter 6
Subqueries
Objectives
4 Correlated subqueries
SELECT producerC#
FROM movies
WHERE (title, year) IN
(SELECT movieTile, movieYear
FROM starsIn
WHERE starName = ‘Harrison Ford’)
Note: You can not run the above code in SQL Server
but in Oracle, DB2, MySQL
4 - Correlated Sub-Queries
SELECT name
FROM movieExec,
(SELECT producerC#
FROM movies, starsIn
WHERE title = movieTitle
AND year = movieYear
AND starName = ‘Harrison Ford’
) as Prod
WHERE cert# = Prod.producerC#
ĐẠI HỌC FPT CẦN THƠ
Chapter 6
Full relation operations
Objectives
1 Aggregation operators
2 Grouping
4 HAVING clauses
1 - Aggregate Functions
OR Aggregate Operators
STUDENT
CLASS_ID ID NAME
106 1A
106 2B
107 3C
107 4D
5E
6F
7G
8H
Syntax:
SELECT column, group_function (column)
FROM table
[WHERE conditions]
[GROUP BY group_by_expression]
[HAVING conditions]
[ORDER BY {column [ASC | DESC] ,…} ]
1
Select ROWS (with WHERE clause) first
2
ROWS are grouped (GROUP BY clause)
3
Groups matching the HAVING clause are
displayed
Example: HAVING
Chapter 6
Database Modifications & Transactions
Objectives
1 Insertions
2 Deletions
3 Updates
5 Exercises
Review: Sub-languages of T-SQL
T - SQL
DDL
(Data Definition Language)
DML
(Data Manipulation Language)
DCL
(Data Control Language)
Review: Sub-languages of T-SQL
Review:
DDL – Data Definition Language
Review:
DML – Data Manipulation Language
1 – Insertion with INSERT command
Example: Insert command
Example: Insert command
2 – Deletions with DELETE command
Example: Delete command
Example: Delete command
Differences between DELETE
and TRUNCATE
$500
ACC: 3209
$500
ACC: 3208
Example: Transaction