Structured Query Language (SQL) Part 1rychly/public/docs/ADES.sql-1/ADES.sql-1.print.pdf · v8,...
Transcript of Structured Query Language (SQL) Part 1rychly/public/docs/ADES.sql-1/ADES.sql-1.print.pdf · v8,...
Structured Query Language (SQL) – Part 1
Marek [email protected]
Strathmore University,@iLabAfrica
&
Brno University of Technology,Faculty of Information Technology
Advanced Databases and Enterprise Systems14 September 2015
Marek Rychly Structured Query Language (SQL) – Part 1 — ADES, 14 September 2015 1 / 35
Outline
1 Structured Query Language (SQL)History of SQL and Relational DBMSSELECT StatementSub-queries in the SELECT Statement
Marek Rychly Structured Query Language (SQL) – Part 1 — ADES, 14 September 2015 2 / 35
Structured Query Language (SQL)History of SQL and Relational DBMSSELECT StatementSub-queries in the SELECT Statement
Structured Query Language (SQL)
query, data manipulation and definition relational db. languageto create database and relation structures
to perform data management task (insert, update, delete)
to perform both simple and complex queries
standard both by specification and by usage in practicespecified by International Organization for Standardization (ISO)
utilized by DBMS vendors (Oracle, IBM, open-source, etc.)
a transform-oriented language based on relational algebra(an expression describes how to transform input relations to an output relation)
a non-procedural (declarative) language(it describes what operations of relational algebra should be applied on input data,not how it should be done in terms of input data retrieval, their processing, etc.)
Marek Rychly Structured Query Language (SQL) – Part 1 — ADES, 14 September 2015 4 / 35
Structured Query Language (SQL)History of SQL and Relational DBMSSELECT StatementSub-queries in the SELECT Statement
Early History of SQL
1970 E. F. Codd, from IBM labs., introduced the relational database model(including relational algebra and relational calculus)
1974 D. Chamberlin, also from IBM labs., introduced SEQUEL(a relational database language called the Structured English Query Language)
1976 Chamberlin et al. introduces SEQUEL/2(later, it has been renamed to SQL for legal reasons)
IBM produced a prototype DBMS based on SEQUEL/2, called System R(also based on SQUARE (Specifying Queries As Relational Expressions), 1975)
1978–79 SDL/RSI company introduced a first versions of Oracle, V1 and V2(Oracle V2 was the first commercial RDBMS; RSI became Oracle Corp. in 1982)
1987-89 The first/initial ISO standard of SQL was published and extended later(many relational features were missing, implemented by vendors in various ways)
Marek Rychly Structured Query Language (SQL) – Part 1 — ADES, 14 September 2015 5 / 35
Structured Query Language (SQL)History of SQL and Relational DBMSSELECT StatementSub-queries in the SELECT Statement
Current History of SQL
1992 SQL-92, the first major/usable revision to the ISO standard occurred(still, it did not cover many advanced features implemented by these days DBMSs)
1999 SQL:1999 published by ISO, SQL formalized including advanced features(such as support object-oriented data management)
Oracle8i released as an RDBMS inter-operating better within the Internet
2003 SQL:2003 published by ISO, consisted of Core SQL and SQL Packages(the core mandatory, packages optional, e.g., for object and XML data, etc.)
2003-13 SQL/MM (SQL multimedia and application packages) by ISO(full-text, spatial, and still image data, data mining, history, meta-data registries)
2006 SQL:2006 as Part 14 of SQL:2003 published by ISO(a package that defines ways in which SQL can be used in conjunction with XML)
2008 SQL:2008 published by ISO, another restructuring of the specification(Framework, Foundation, Object Language Bindings, Information and DefinitionSchemas, SQL Routines and Types Using Java, XML-Related Specifications, etc.)
2011 SQL:2008 published by ISO as an update of SQL:2008(new features include improved support for temporal databases)
Marek Rychly Structured Query Language (SQL) – Part 1 — ADES, 14 September 2015 6 / 35
Structured Query Language (SQL)History of SQL and Relational DBMSSELECT StatementSub-queries in the SELECT Statement
Early History of RDBMS Systems
(adopted from “History of RDBMS, Data-e-Education, 2014”)
Marek Rychly Structured Query Language (SQL) – Part 1 — ADES, 14 September 2015 7 / 35
Structured Query Language (SQL)History of SQL and Relational DBMSSELECT StatementSub-queries in the SELECT Statement
Current History of RDBMS Systems
IBM Peterlee Relational Test Vehicle
IBM IS1
BAY AREA PARK
CODD RIVER
RELATIONAL CREEK
CODD RIVER
BAY AREA PARK
1970s
1980s1990s
2000s
2010s
v1, 1992
v1.0, 1987
v4.0, 1990 v10, 1993
v1, 1987 v2, 1989v3, 2011
v11.5, 1996 v11.9, 1998
v12.0, 1999 v12.5, 2001 v12.5.1, 2003 v15.0, 2005 v16.0, 2012
v1, 1989
v2, 1993
v1.0, 1980s
v5.x, 1970s
v6.0, 1986 OpenIngres 2.0, 1997 vR3, 2004
v1, 1995 v6, 1997 v7, 2000
v8, 2005 v9, 2010
v9.0, 2006 v10, 2010
v4.0, 1990 v5.0, 1992 v6.0, 1994
v9.0, 2000 v10, 2005 v11, 2007
v4.21, 1993 v6, 1994 v7, 1998
v8, 1997
v3.1, 1997 v3.21, 1998 v3.23, 2001 v4, 2003 v4.1, 2004 v5, 2005 v5.1, 2008
v5.5, 2010
v8i, 1999 v9i, 2001 v10g, 2003 v10gR2, 2005 v11g, 2007 v11gR2, 2009
v8, 2000 v9, 2005 v10, 2008 v11, 2012
v3, 1995 v4, 1997 v5, 1999 v10, 2001 v11, 2003 v12, 2007 v14, 2010
v3, 1983 v4, 1984 v5, 1985
v1, 1983
v5.1, 1986
v3, 1993
v1, 1983 v2, 1988 v3, 1993 v4, 1994
v5, 1996
v6, 1999 v7, 2001 v8, 2003 v9, 2006
alpha, 1979
v1.0, 1981
v6.1, 1997 v8.1, 1998 v10.2, 2008
v5.1, 2004 v6.0, 2005 v6.2, 2006 v12, 2007 v13.0, 2009
v13.10, 2010 v14.0, 2012
v4, 1995
v5, 1997
v6, 1999
v1, 1991 v2, 1997
v3, 1999 v4, 2001
v1.6, 2001 v1.7, 2002
v1.8, 2005
v3.0, 1988
v2.0, 2010
v5, 2010
v7, 2001 v8, 2004 v9, 2007
v10, 2010
v7, 1992
v7.0, 1995
v2, 1979
v1, 2003 v1.5, 2009
v2, 2012
v6.5, 1995
codebrand
v11, 1995v12, 1999
v15, 2009
v12c, 2013
v1, 1988 v2, 1992 v4, 1992
v6, 2008 v7, 2010
Ingres
VectorWise
MonetDB
Netezza
Greenplum
PostgreSQL
Red Brick
Microsoft SQL ServerH-Store
Informix
VoltDB
Vertica
db40
Sybase ASE
Sybase IQ
SQL Anywhere
Access
Oracle
Infobright
MySQL
TimesTen
Paradox
Teradata
Empress Embedded
RDB
DB2 for iSeries
Derby
Transbase
DB2 for z/OSDB2 for VM
Solid DB
EXASolution
dBase
Firebird
DB2 for LUWHSQLDB
BerkelyDB
SQLite
HANA
MaxDB
Nonstop SQL
AdabasD
MariaDB
v10, 2013
SQL/DS
DB2 for VM
DB2 UDB
Transbase(Transaction Software)
TinyDB
Berkeley DB
DB2 MVS
Solid DB
Gamma (Univ. Wisconsin) Mariposa (Berkeley)
dBase (Ashton Tate)
DB2
NDBM
GDBM
SQLite
HSQLDB
DBM
VDN/RDS DDB4 (Nixdorf)SAP DB MaxDB
Borland
Siemens
dBase Inc.
EMC
NCR Teradata
SAP
IBM
Oracle
Oracle
Oracle
IBM
Oracle
System-R (IBM)
AdabasD(Software AG)
SAP HANA
P*TIME
SAP
REDABAS (Robotron)
DABA (Robotron, TU Dresden)
Borland
Corel
EXASolution
InterBaseAshton Tate Firebird
HP
HP
Compaq
DB2 z/OS
Powersoft Sybase
System/38
SQL/400 DB2/400
DB2 UDB for iSeries
Sleepycat
Informix IBM
Sun
Oracle
RDB (DEC)
Teradata
Empress Embedded
TimesTen Aster Database
JBMS
Cloudscape Derby
Paradox (Ansa)
Red Brick
Multics Relational Data Store (Honeywell)
Apache Derby
Berkeley Ingres
Ingres
Postgres PostgreSQL
IllustraIBM Informix
MonetDB (CWI)
Greenplum
Volt DB
Netezza
Informix
Sybase ASE
Microsoft SQL ServerMicrosoft Access
MySQL
Sybase IQ
Nonstop SQL(Tandem) Neoview
mSQL
InnoDB (Innobase)
Infobright
H-StoreC-Store
db40
Vertica Analytic DB
VectorWise (Actian)Monet Database System (Data Distilleries)
DATAllegro
Informix
IBM Red Brick Warehouse
Expressway 103
Watcom SQL SQL Anywhere
MariaDB
Key to lines and symbols Felix Naumann, Jana Bauckmann, Claudia Exeler, Jan-Peer Rudolph, Fabian Tschirschnitz
Contact - Hasso Plattner Institut, Potsdam, [email protected]
Design - Alexander Sandt Gra�k-Design, Hamburg
Version 4.0 - August 2013
http://www.hpi.uni-potsdam.de/naumann/projekte/rdbms_genealogy.html
Publishing Date
Genealogy of Relational Database Management Systems
Discontinued Branch (intellectual and/or code)Acquisition Versionsv9, 2006CC
Crossing lines have no special semantics(adopted from “RDBMS Genealogy, Hasso-Plattner-Institut, 2015”)
Marek Rychly Structured Query Language (SQL) – Part 1 — ADES, 14 September 2015 8 / 35
Structured Query Language (SQL)History of SQL and Relational DBMSSELECT StatementSub-queries in the SELECT Statement
Web SQL (SQLite) Relational DBMS
relational DBMS provided by web-browser by HTML5 API(based on the underlying SQLite RDBMS, e.g., SQLite v3.6.1 in Google Chrome)
currently supported only by WebKit-based web-browsers:Chrome, Safari, Opera(API is deprecated by W3C since end of 2010 but it is still supported)
utilized by Try-SQL Editor in W3Schools SQL Tutorialhttp://www.w3schools.com/sql/trysql.asp?filename=trysql_select_all
supports all SQLite features (including triggers)(all but storage management and unsecure features, e.g., ATTACH/VACUUM)
suitable for practising the SQL
examples shown in this lecture will be demonstrated on Try-SQL
the examples will use tables provided by W3Schools SQL Tutorial
Marek Rychly Structured Query Language (SQL) – Part 1 — ADES, 14 September 2015 9 / 35
Structured Query Language (SQL)History of SQL and Relational DBMSSELECT StatementSub-queries in the SELECT Statement
SQL: DDL and DML Languages
SQL can be divided into two partsData Definition Language (DDL)(for defining the database structure and controlling access to the data)
Data Manipulation Language (DML)(for retrieving and updating data)
SQL statements consist of two types of words(both are used as case-insensitive, contrary to data-typed string values)
reserved words(select, from, where, order by, group by, create, table, row, primary key, etc.)
user-defined words(names of tables, columns, indices, views, etc.)
individual SQL statements/commands separated by semicolon “;”
non-numeric literals (constants) are enclosed in single quotes(for example: 123, �1:23, ’John Smith’, date(’now’), date(’2015-09-114’), . . . )
Marek Rychly Structured Query Language (SQL) – Part 1 — ADES, 14 September 2015 10 / 35
Structured Query Language (SQL)History of SQL and Relational DBMSSELECT StatementSub-queries in the SELECT Statement
SELECT Statement
SELECT [DISTINCT | ALL] {* | columnExpression [AS newName]] [, ...]}FROM tableName [alias] [, ...]
[WHERE condition][GROUP BY columnName [, ...]] [HAVING condition][ORDER BY columnNameOrNewName [DESC | ASC] [, ...]]
columnExpression is an expression with column names(in the resulting relation, the result of the expression can be known as newName)
tableName is the name of an input table, columnName of a table column(in the select statement, the table can be referred by alias)
condition is a logical expr. with table names/aliases and column names
columnNameOrNewName is the name of a column or a column expr. above
The statement selects such data from input tables that meet “where” condition.For such, all or distinct, data rows, column expressions are computed. It thecase of the expression with an aggregate function, the data are grouped by“group by” condition, the aggregate function is applied, and results is checkedto meet “having” condition. Finally, the resulting data are ordered by values ofgiven columns in asceding or descending order.
Marek Rychly Structured Query Language (SQL) – Part 1 — ADES, 14 September 2015 11 / 35
Structured Query Language (SQL)History of SQL and Relational DBMSSELECT StatementSub-queries in the SELECT Statement
Example of SELECT Statement
SELECT C.Country, count(C.CustomerID) AS CustomersInCountryFROM Customers CWHERE C.CustomerName LIKE ’% %’GROUP BY C.Country HAVING CustomersInCountry >= 3ORDER BY CustomersInCountry DESC;
Take all customers that have a space in their names,
group them into groups according to their country,(all members of each group will have the same value of the country column)
take just such groups that have at least 3 members (rows),
for each group, print country value and number of group members,
ordered by the number of group members descending.
Marek Rychly Structured Query Language (SQL) – Part 1 — ADES, 14 September 2015 12 / 35
Structured Query Language (SQL)History of SQL and Relational DBMSSELECT StatementSub-queries in the SELECT Statement
Clauses in the SELECT Statement
SQL SELECT statements are processed in the following order:(note that the processing order is different from the order of the clauses in the statement)
FROM – specifies the table or tables to be used,
WHERE – filters the rows subject to some condition,
GROUP BY – forms groups of rows with the same column value,
HAVING – filters the groups subject to some condition,
SELECT – specifies which columns are to appear in the output.
ORDER BY – specifies the order of the output.
SELECT and FROM clauses are mandatory, others are optional.
Marek Rychly Structured Query Language (SQL) – Part 1 — ADES, 14 September 2015 13 / 35
Structured Query Language (SQL)History of SQL and Relational DBMSSELECT StatementSub-queries in the SELECT Statement
The SELECT Clause
SELECT [DISTINCT | ALL] {* | columnExpression [AS newName]] [, ...]}
Specifies which columns are to appear in the output.
Represents the Projection operation of relational algebra.
The column expression consists ofcolumn names from tables in FROM clause,constants,expressions of the column names, constants, and operations.(e.g., arithmetic operations; string operations such as concatenation; etc.)
Star symbol “*” means all possible columns.(e.g., SELECT * FROM Customers; lists all values form the “Customers” table)
By default, SELECT does not eliminate duplicities.To eliminate duplicities, DISTINCT has to be used (ALL is default).(duplicate data may come from input tables or may be produced by the projection)
Marek Rychly Structured Query Language (SQL) – Part 1 — ADES, 14 September 2015 14 / 35
Structured Query Language (SQL)History of SQL and Relational DBMSSELECT StatementSub-queries in the SELECT Statement
Examples on SELECT Clause
List all cities of customers including duplicities.
SELECT City FROM Customers;
List all cities of customers without duplicities.
SELECT DISTINCT City FROM Customers;
List all columns of all rows from table “Empployees”.
SELECT * FROM Employees;
List full names and age in days of all employees.
SELECT FirstName || ’ ’ || LastName AS FullName,julianday(’now’) - julianday(BirthDate) AS AgeInDays
FROM Employees;
Marek Rychly Structured Query Language (SQL) – Part 1 — ADES, 14 September 2015 15 / 35
Structured Query Language (SQL)History of SQL and Relational DBMSSELECT StatementSub-queries in the SELECT Statement
The WHERE Clause
[WHERE condition]
Filters the rows subject to some condition.Represents the Selection operation of relational algebra.The condition consists of
constants, column names from tables in FROM clause,predicates applied on of the constants and column names,logical operators AND, OR, and NOT, with parentheses
There are several predefined predicates for testingcomparison: =, <>, <, >
(compare the value of one expression to the value of another expression)range: x BETWEEN a AND b, x NOT BETWEEN a AND b
(test whether the value falls within a specified range of values)set membership: x IN (a, b, c), x NOT IN (a, b, c)
(test whether the value equals one of a set of value)pattern match: x LIKE ’pattern’, x NOT LIKE ’pattern’
(test whether a string matches a specified pattern, usually case-sensitive)null: x IS NULL, x IS NOT NULL
(test whether a column has a Null (unknown) value)Marek Rychly Structured Query Language (SQL) – Part 1 — ADES, 14 September 2015 16 / 35
Structured Query Language (SQL)History of SQL and Relational DBMSSELECT StatementSub-queries in the SELECT Statement
Pattern Matching an Null Search in WHERE Clause
columnExpression [NOT] LIKE ’pattern’ [ESCAPE ’escapeChar’]
Pattern is a text literal with two special pattern-matching symbols:
% represents any sequence of zero or more characters (a wildcard),_ represents any single character.
The special symbols can be used as common characters if escaped byescapeChar. For example:
SELECT * FROM Sale WHERE Discount LIKE ’%15#%’ ESCAPE ’#’;
columnName IS [NOT] NULL
The expression is true if and only if the column value is (not) Null.Null value used as operand in any operation will result into Null.
Marek Rychly Structured Query Language (SQL) – Part 1 — ADES, 14 September 2015 17 / 35
Structured Query Language (SQL)History of SQL and Relational DBMSSELECT StatementSub-queries in the SELECT Statement
The ORDER BY Clause
[ORDER BY columnNameOrNewName [DESC | ASC] [, ...]]
Rows of an SQL query result table are ordered.(contrary to results of relational algebra operations)
The ORDER BY clause allows the retrieved rows to be ordered.
If unspecified by ORDER BY clause, the order is undefined.(usually, in the such cases, resulting rows are ordered by primary keys)
Result can be ordered by multiple columns and for eachASC means ascending order on a given column,
DESC means descending order on a given column.
The ORDER BY clause has to be the last clause of the statement.
Marek Rychly Structured Query Language (SQL) – Part 1 — ADES, 14 September 2015 18 / 35
Structured Query Language (SQL)History of SQL and Relational DBMSSELECT StatementSub-queries in the SELECT Statement
Examples on WHERE and ORDER BY Clauses
List employees whose first names start with A and were born in February.
SELECT * FROM EmployeesWHERE FirstName LIKE ’A%’ AND BirthDate LIKE ’____-02-__’;
List products with non-empty supplier and price between $10 and $20arranged in descending order of price.
SELECT * FROM Products WHERE Price BETWEEN 10 AND 20AND SupplierID IS NOT NULL ORDER BY Price DESC;
List full names and birth dates of employees in ascending order of names.
SELECT LastName || ’, ’ || FirstName as FullName, BirthDateFROM Employees ORDER BY FullName ASC;
or, alternatively, . . . ORDER BY LastName ASC, FirstName ASC;
List suppliers that are neither from UK nor from USA.
SELECT * FROM Suppliers WHERE Country NOT IN (’UK’,’USA’);
Marek Rychly Structured Query Language (SQL) – Part 1 — ADES, 14 September 2015 19 / 35
Structured Query Language (SQL)History of SQL and Relational DBMSSELECT StatementSub-queries in the SELECT Statement
Aggregation and Grouping in the SELECT Statement
SELECT [columnExpression [, ...]]{aggregateFunction(columnName) | COUNT(*)}, [, ...]
...[GROUP BY columnName [, ...]] [HAVING condition]...
With an aggr. function in SELECT clause the data are aggregated.
The aggregate function applies on all values in a given column.all non-Null values in the case of a particular column name,all values including Null values in the case of COUNT(*) function,all distinct values in the case of DISTINCT word preceding thecolumn name in the operand of the aggregate function.
By GROUP BY clause, the aggr. function aggregates data foreach group of rows with identical values of grouped columns.
In the case of non-aggregated columns in the SELECT clause,those columns must appear also in the GROUP BY clause.
By HAVING clause, aggregation results are filtered by condition.Marek Rychly Structured Query Language (SQL) – Part 1 — ADES, 14 September 2015 20 / 35
Structured Query Language (SQL)History of SQL and Relational DBMSSELECT StatementSub-queries in the SELECT Statement
Aggregate Functions
The SQL standard by ISO defines five aggregate functions:COUNT – returns the number of values in a specified column,
SUM – returns the sum of the values in a specified column,
AVG – returns the average of the values in a specified column,
MIN – returns the smallest value in a specified column,
MAX – returns the largest value in a specified column.
Relational DBMSs usually provide additional aggregate functions.(e.g., group_concat(column,separator), stddev(column), variance(column), etc.)
To filter by aggregate function results, a condition has to be putinto HAVING clause, not into WHERE clause.(the HAVING condition may contain the aggregate functions applications or aliasesof columns from SELECT clause where the aggregation functions are)
Marek Rychly Structured Query Language (SQL) – Part 1 — ADES, 14 September 2015 21 / 35
Structured Query Language (SQL)History of SQL and Relational DBMSSELECT StatementSub-queries in the SELECT Statement
Examples on GROUP BY Clause
For each product ID get total number of its orders, total sum and maximalordered quantity, in the cases when the total sum is 100 units or above.
SELECT ProductID, COUNT(*), SUM(Quantity), MAX(Quantity)FROM OrderDetailsGROUP BY ProductID HAVING SUM(Quantity) >= 100;
Get ID and name of products that have their prices the same or nearlysame with maximal difference of $5 as the average price of all products.
SELECT ProductID, ProductName FROM ProductsWHERE Price BETWEEN (SELECT AVG(Price)-5 FROM Products) AND (SELECT AVG(Price)+5 FROM Products);
Marek Rychly Structured Query Language (SQL) – Part 1 — ADES, 14 September 2015 22 / 35
Structured Query Language (SQL)History of SQL and Relational DBMSSELECT StatementSub-queries in the SELECT Statement
The FROM Clause and Multi-Table Queries
... FROM tableName [alias] [, ...] ...
The FROM clause of a single query can refer to multiple tables.(then, the query will process data from all the tables)
To combine columns from several tables in the query into a resulttable we need to use a Join operation of relational algebra.
In multi-table query, columns values can be accessed astableName.columnName
(this is necessary if there is a column of the same name in several tables)
There are several different types of the Join operation.((inner) cross join, (Theta) join, natural join, left/right/full outer join, etc.)
Marek Rychly Structured Query Language (SQL) – Part 1 — ADES, 14 September 2015 23 / 35
Structured Query Language (SQL)History of SQL and Relational DBMSSELECT StatementSub-queries in the SELECT Statement
(Inner) JOIN and CROSS JOIN Operations
... FROM OrderDetails, Products ...
... FROM OrderDetails JOIN Products ...
... FROM OrderDetails INNER JOIN Products ...
... FROM OrderDetails CROSS JOIN Products ...
The clauses above produce the same Cartesian product of tables.(i.e., they will pair all rows from the first table with all row from the second table)
To match particular rows, join criteria must be defined.(then, it wont be a plain Cartesian product but a Theta join as in relational algebra)
In multi-table query, columns values can be accessed astableName.columnName
(this is necessary if there is a column of the same name in two or more tables)
Join criteria can be put into WHERE clause or into FROM clause.(in the FROM clause, the join criteria are prefixed by “ON” word)
Marek Rychly Structured Query Language (SQL) – Part 1 — ADES, 14 September 2015 24 / 35
Structured Query Language (SQL)History of SQL and Relational DBMSSELECT StatementSub-queries in the SELECT Statement
Examples of Multi-table Queries
List details of all orders where the ordered quantity is 10 units and more.
SELECT * FROM OrderDetails, ProductsWHERE OrderDetails.ProductID = Products.ProductIDAND Quantity >= 10;
or, alternatively,
SELECT * FROM OrderDetails JOIN ProductsON OrderDetails.ProductID = Products.ProductIDWHERE Quantity >= 10;
List all orders with their details, details of ordered products and suppliersof the products.
SELECT * FROM OrdersJOIN OrderDetails ON OrderDetails.OrderID = Orders.OrderIDJOIN Products ON Products.ProductID = OrderDetails.ProductIDJOIN Suppliers ON Products.SupplierID = Suppliers.SupplierID;
Marek Rychly Structured Query Language (SQL) – Part 1 — ADES, 14 September 2015 25 / 35
Structured Query Language (SQL)History of SQL and Relational DBMSSELECT StatementSub-queries in the SELECT Statement
NATURAL (Inner) JOIN Operation
... FROM OrderDetails NATURAL JOIN Products ...
... FROM OrderDetails NATURAL INNER JOIN Products ...
The clauses above produce the same Natural join of tables.
Join criteria are equalities of columns of the same names.(see the Equi-join and Natural join operations in relational algebra)
For particular choice of columns of the same names, it is possibleto use JOIN USING with a list of columns for the join criteria.For example,
SELECT * FROM Orders JOIN OrderDetails USING (OrderID);
Marek Rychly Structured Query Language (SQL) – Part 1 — ADES, 14 September 2015 26 / 35
Structured Query Language (SQL)History of SQL and Relational DBMSSELECT StatementSub-queries in the SELECT Statement
OUTER JOIN Operations
... FROM Customers LEFT OUTER JOIN OrdersON Customers.CustomerID = Orders.CustomerID ...
... FROM Customers LEFT OUTER JOIN Orders USING (CustomerID) ...
... FROM Customers NATURAL LEFT OUTER JOIN Orders ...
... FROM Orders NATURAL RIGHT OUTER JOIN Customers ...
The clauses above produce the same results.(they are a Theta-, Equi-, and Natural left-outer join operations in relational algebra)
Left (right) outer join takes all rows from the left (right) table andmatches them with respective rows of the other table if possible,or with Null values otherwise.(see the Outer join operations in relational algebra)
As the right- join and full-outer join ops. can be computed byleft-outer join op(s)., they may not be supported by some RDBMS.(e.g., SQLite, that is also Web-SQL supports only the left outer join operation)
Marek Rychly Structured Query Language (SQL) – Part 1 — ADES, 14 September 2015 27 / 35
Structured Query Language (SQL)History of SQL and Relational DBMSSELECT StatementSub-queries in the SELECT Statement
Examples on OUTER JOIN Operations
List all orders with their customers and also customers without any orders.
SELECT * FROM Customers NATURAL LEFT OUTER JOIN Orders;
List all possible replacements of products with the same price.
SELECT Original.ProductID AS OrigID,Original.ProductName AS OrigName,Alternative.ProductID AS AltID,Alternative.ProductName AS AltName,Price
FROM Products Original LEFT OUTER JOIN Products AlternativeUSING (Price)
WHERE OrigID <> AltID;
Marek Rychly Structured Query Language (SQL) – Part 1 — ADES, 14 September 2015 28 / 35
Structured Query Language (SQL)History of SQL and Relational DBMSSELECT StatementSub-queries in the SELECT Statement
Sub-queries in SELECT Statements (Sub-selects)
It is possible to perform an inner SELECT statement (a nestedquery) and to use its result(s) in an outer SELECT statement.
From the result(s) produced, we can distinguishscalar sub-queries – return a single column&row (a single value),row sub-queries – return a single row with multiple columns,table sub-queries – return multiple rows&columns (a table).
A scalar sub-query can be used when a single value is needed.(for example, in WHERE clause as an operand of a predicate, e.g, with equality)
Scalar sub-queries are useful especially to integrate results ofaggregate functions into complicated (outer) queries.For example,
SELECT ProductID, ProductName FROM ProductsWHERE Price BETWEEN (SELECT AVG(Price)-5 FROM Products)
AND (SELECT AVG(Price)+5 FROM Products);
Marek Rychly Structured Query Language (SQL) – Part 1 — ADES, 14 September 2015 29 / 35
Structured Query Language (SQL)History of SQL and Relational DBMSSELECT StatementSub-queries in the SELECT Statement
Row and Table Sub-queries in SELECT Statements
Row and table sub-queries can be used with the following predicates:(these predicates exist also in their negative forms, prefixed by “NOT” word, e.g., NOT IN)
IN to test if a value is in a result set a particular sub-query
ALL to test if a predicate holds for all results of a particular sub-query
ANY to test if a predicate holds for at least one result of a sub-query1
EXISTS to test if a result set a particular sub-query is not empty
ALL and ANY (SOME) may not be supported by some RDBMS.(for example, they are not supported by SQLite, that is also by Web SQL)
1ANY predicate is also known as SOME predicate; ANY and SOME are synonymsMarek Rychly Structured Query Language (SQL) – Part 1 — ADES, 14 September 2015 30 / 35
Structured Query Language (SQL)History of SQL and Relational DBMSSELECT StatementSub-queries in the SELECT Statement
Examples of Sub-queries
List all employees that did not take any orders.
SELECT * FROM EmployeesWHERE EmployeeID NOT IN (SELECT EmployeeID FROM Orders);
or, alternatively,
SELECT * FROM Employees E WHERE NOT EXISTS(SELECT 1 FROM Orders O WHERE O.EmployeeID = E.EmployeeID);
Get product with the highest price.
SELECT * FROM ProductsWHERE Price >= ALL (SELECT Price FROM Products);
or, alternatively,
SELECT * FROM ProductsWHERE Price >= (SELECT MAX(Price) FROM Products);
Marek Rychly Structured Query Language (SQL) – Part 1 — ADES, 14 September 2015 31 / 35
Structured Query Language (SQL)History of SQL and Relational DBMSSELECT StatementSub-queries in the SELECT Statement
Set Operations
Table sub-queries and the whole selects can be combined by setoperations as they are defined in the relational algebra:(results combined by the set operations have to be union-compatible)
UNION to merge results of both select statements into a single results
INTERSECT to filter rows that are common to results of both statements
EXCEPT to filter rows that appear in results of the first statement but notin results of the second one
There are modifiers2 of the set ops. that can be put after their names:ALL – to include duplicate rows in the result, e.g., UNION ALL
CORRESPONDING – to perform a given set operation only on thecolumns that are common (the same names) to results of both statements
CORRESPONDING BY – to perform an op. on the named columns only
2the modifiers may not be supported by some RDBMS, e.g., by SQLite/Web SQLMarek Rychly Structured Query Language (SQL) – Part 1 — ADES, 14 September 2015 32 / 35
Structured Query Language (SQL)History of SQL and Relational DBMSSELECT StatementSub-queries in the SELECT Statement
Examples on Set Operations
List cities where are both customers and suppliers.
SELECT City, Country FROM SuppliersINTERSECTSELECT City, Country FROM Customers;
Get all customers that are from Germany or UK countries or that haveordered something, or both, and count their orders or residences in thecountries.(that is, the count for a customer who is from UK and made one order will be 2while the count for a customer outside Europe who made three orders will be 3)
SELECT CustomerID, COUNT(*) AS Active FROM (SELECT CustomerID FROM CustomersWHERE Country IN (’Germany’, ’UK’)UNION ALLSELECT CustomerID FROM Orders
) GROUP BY CustomerID ORDER BY Active DESC;
Marek Rychly Structured Query Language (SQL) – Part 1 — ADES, 14 September 2015 33 / 35
Summary
Summary
SQL consists of DDL and DML languages.(DDL is for schema definition, while DML is for queries and data manipulation)
SELECT statement has several clauses.(SELECT and FROM clauses are mandatory, others are optional)
Results of SELECT queries are ordered multi-sets.(sets that keep order of its elements and may contain duplicities)
SELECT queries can be used as nested queries in other SELECTstatements.
In the next lecture:Structured Query Language (SQL) – Part 2(Common Table Expressions/CTE queries, INSERT/UPDATE/DELETE, DDL,indices, triggers, etc.)
Marek Rychly Structured Query Language (SQL) – Part 1 — ADES, 14 September 2015 34 / 35
Thanks
Thank you for your attention!
Marek Rychly<[email protected]>
Marek Rychly Structured Query Language (SQL) – Part 1 — ADES, 14 September 2015 35 / 35