SQL Review
SQL Review
Datawarehousing
SQL: basic concepts revisited
Introduction
SQL: basic concepts revisited - Introduction
Overview
• (Microsoft) SQL
– Working with 1 table: SELECT, Statististical functions,
GROUP BY
– Working with > 1 tables: JOIN, UNION, subquery's,
correlated subquery's
– Modifying data: insert, update, delete
– Views
SQL: basic concepts revisited - Introduction
SQL Server
• SQL Server:
– Management
• Installation, configuration and security of SQL Server.
• Database creation
• Database management: backup, restore, ...
• Use SQL Server Management Studio
SQL: basic concepts revisited - Introduction
Writing queries
• Use SQL Server Management Studio
SQL: basic concepts revisited - Introduction
Help!
• The help menu offers online help about Microsoft SQL
Year Name
• Standards Comments
1986 SQL-86 First formalized by ANSI.
1989 SQL-89 Minor revision that added integrity constraints, adopted as FIPS 127-1.
1992 SQL-92 Major revision (ISO 9075), Entry Level SQL-92 adopted as FIPS 127-2.
Added regular expression matching, recursive queries (e.g. transitive closure), triggers, support for procedural and control-of-flow statements, non-scalar types, and some
1999 SQL:1999
object-oriented features (e.g. structured types). Support for embedding SQL in Java (SQL/OLB) and vice versa (SQL/JRT).
2003 SQL:2003 Introduced XML-related features (SQL/XML), window functions, standardized sequences, and columns with auto-generated values (including identity-columns).
ISO/IEC 9075-14:2006 defines ways that SQL can be used with XML. It defines ways of importing and storing XML data in an SQL database, manipulating it within the
2006 SQL:2006 database, and publishing both XML and conventional SQL-data in XML form. In addition, it lets applications integrate queries into their SQL code with XQuery, the XML
Query Language published by the World Wide Web Consortium (W3C), to concurrently access ordinary SQL-data and XML documents.[40]
2008 SQL:2008 Legalizes ORDER BY outside cursor definitions. Adds INSTEAD OF triggers. Adds the TRUNCATE statement. [41]
2011 SQL:2011 Adds temporal data definition and manipulation.
2016 SQL:2016 Adds row pattern matching, polymorphic table functions, JSON.
SQL: basic concepts revisited - Introduction
Source: [Link]
SQL: basic concepts revisited - Introduction
SQL - Overview
• SQL consists of 3 sub languages
– Data Definition Language (DDL)
• creation of a database, defining database objects (tables, stored procedures,
views,…)
• CREATE, ALTER, DROP
– Data Manipulation Language (DML)
• Querying and manipulating data in a database
• SELECT, INSERT, UPDATE, DELETE
– Data Control Language (DCL)
• Data security and authorisation
• GRANT, REVOKE, DENY
SQL: basic concepts revisited - Introduction
SQL - Overview
• Additional language elements: operators, functions,
control of flow (dialects!)
SELECT
SQL: basic concepts revisited - Select
SELECT
• SELECT clause: specification of the columns
– All columns from table: use *
• SELECT *
– Specific columns: use columns names or expression
• SELECT column1 , column2, column3*column4, …
SQL: basic concepts revisited - Select
SELECT
• Example: Show all data of all products
SELECT *
FROM Products
SQL: basic concepts revisited - Select
SELECT
• Example: Show for all a products productID,
name and unitprice
SELECT productid, productname, unitprice
FROM Products
SQL: basic concepts revisited - Select
SELECT … WHERE
• WHERE clause
– Specification of conditions for individual rows
• Example: Show productid, productname and unitprice of
all products from category 1
SELECT productid, productname, unitprice
FROM Products
WHERE categoryID = 1
SQL: basic concepts revisited - Select
SELECT … WHERE
• Use of literals
– Numeric values: ... WHERE categoryID = 1
– Alphanumeric values: ... WHERE productName = 'Chai'
– Dates: ... WHERE orderDate = '4/15/2018' (15th april 2018)
SQL: basic concepts revisited - Select
SELECT … WHERE
• Conditions for rows
– Comparison operators
– Wildcards
– Logical operators
– Interval of specific values
– List of values
– Unknown values
– Use brackets () to overrule priority rules and enhance readability
SQL: basic concepts revisited - Select
SELECT … WHERE
• Comparison operators
– =, >, >=, <, <=, <>
– Example: Show productID, name, units in stock for all products
with less than 5 units in stock
SELECT productid, productname, unitprice
FROM Products
WHERE UnitsInStock < 5
SELECT … WHERE
• Wildcards (searching for patterns)
– Always in combination with operator LIKE, NOT LIKE
– Wildcard symbols:
• % arbitrary sequence of 0, 1 or more characters
• _ 1 character
• [ ] 1 character in a specified range
• [^] every character not in the specified range
– Example: Show productID and name of the products for which
the second letter is in the range a-k
SELECT productid, productname
FROM Products
WHERE productname LIKE '_[a-k]%'
SQL: basic concepts revisited - Select
SELECT … WHERE
• Logical operators
– OR, AND, NOT (ascending priority)
– Example
SELECT ProductID, ProductName, SupplierID, UnitPrice
FROM Products
WHERE ProductName LIKE 'T%' OR (ProductID = 46 AND UnitPrice > 16.00)
SQL: basic concepts revisited - Select
SELECT … WHERE
• Values in an interval
– BETWEEN, NOT BETWEEN
– Example: Select the products (name and unit price) for which the
unit price is between 10 and 15 euro (boundaries included)
SELECT ProductName, UnitPrice
FROM Products
WHERE UnitPrice BETWEEN 10 AND 15
SQL: basic concepts revisited - Select
SELECT … WHERE
• List of values
– IN, NOT IN
– Example: Show ProductID, ProductName and SupplierID of the
products supplied by suppliers with ID 1, 3 or 5
SELECT ProductID, ProductName, SupplierID
FROM Products
WHERE SupplierID in (1,3,5)
SQL: basic concepts revisited - Select
SELECT … WHERE
• Test for unknow (or empty) values
– IS NULL, IS NOT NULL
• NULL values occur if no value has been specified for a column when creating a
record
• A NULL is not equal to 0 (for numerical values), blank or empty string
(for character values)!
• NULL fields are considered as equal (for e.g. testing with DISTINCT)
• If a NULL value appears in an expression the result is always NULL
– Example: Select suppliers from an unknown region
SELECT … WHERE
• Be careful with NULL!
SELECT CompanyName, Region SELECT CompanyName, Region
FROM Suppliers FROM Suppliers
WHERE Region <> 'OR' WHERE Region <> 'OR' OR Region IS NULL
SQL: basic concepts revisited - Select
SELECT … ORDER BY
• Sorting of data
– ORDER BY clause
• Sorting according to one or more sorting criteria
• Each sorting criterion can be specified by either a column name, an expression or a
sequence number that corresponds to the order of columns in the SELECT clause
(starting from 1)
• Sorting criteria are evaluated left to right
• Default sort occurs in ascending order (ASC: default), if descending order is required
specify DESC after the criterion
– Example: Show an alphabetic list of product names
SELECT ProductName
FROM Products
ORDER BY ProductName -- or ORDER BY 1
SQL: basic concepts revisited - Select
SELECT … ORDER BY
– Example: Show productid, name, categoryID of the products
sorted by categoryID. If the category is the same products with
the highest price appear first.
SELECT ProductID, ProductName, CategoryID, UnitPrice
FROM Products
ORDER BY CategoryID, UnitPrice DESC
SQL: basic concepts revisited - Select
SELECT DISTINCT/ALL
• Uniqueness of rows
• DISTINCT filters out duplicates lines in the output
– ALL (default) shows all rows, including duplicates
– Example: Show all suppliers that supply products
SELECT SupplierID SELECT DISTINCT SupplierID
FROM Products FROM Products
ORDER BY SupplierID ORDER BY SupplierID
SQL: basic concepts revisited - Select
Exercises
-- 1. Give the names of all products containing the word 'bröd' or with a name of 7
characters.
-- 2. Show the productname and the reorderlevel of all products with a level between 10 and
50 (boundaries included)
SQL: basic concepts revisited - Select
Exercises – Solutions
-- 1. Give the names of all products containing the word 'bröd' or with a name of 7
characters.
SELECT ProductName
FROM Products
WHERE ProductName LIKE '%bröd%' or ProductName LIKE '_______'
-- 2. Show the productname and the reorderlevel of all products with a level between 10 and
50 (boundaries included)
SELECT ProductName, ReorderLevel
FROM Products
WHERE ReorderLevel BETWEEN 10 AND 50
SQL: basic concepts revisited - Select
– FORMAT
SELECT *
FROM Orders
WHERE FORMAT(ShippedDate,'dd/MM/yyyy')='10/07/2020'
SQL: basic concepts revisited - Select
String functions
SQL SERVER
SELECT GETUTCDATE()
SELECT SYSDATETIME()
SELECT SYSDATETIMEOFFSET()
SQL: basic concepts revisited - Select
Arithmetic functions
SQL SERVER
GROUP BY
and
statististical functions
SQL: basic concepts revisited – Group by
Statistical functions
• Statistical functions (aka aggregate functions)
– SQL has 5 standard functions
• SUM(expression): sum
• AVG(expression): average
• MIN(expression): minimum
• MAX(expression): maximum
• COUNT(*|[DISTINCT] column name): count
– These functions give one answer per column (or group: see further) and can
never be used in a where-clause
SQL: basic concepts revisited – Group by
• COUNT (column name) – counts the number of not empty fields in a column
• Example: Countasthe
SELECT COUNT(*) number of NOT NULL values in column CategoryID
NumberOfProducts
FROM Products
Transact-SQL dialect
• Some statistical functions only exists in MS Transact-SQL
– STDEV: standard deviation of column values
– VAR: variance of column values
– TOP:
• Example: Select the top 5 of the cheapest products
SELECT TOP 5 ProductID, UnitPrice
FROM Products
ORDER BY UnitPrice
• Show the categories that contain more than 10 products with UnitPrice > 15
SELECT CategoryID, COUNT(ProductID) As NumberOfProductsPerCategory
FROM Products
WHERE UnitPrice > 10
GROUP BY CategoryID
HAVING COUNT(ProductID) > 10
SQL: basic concepts revisited – Group by
WHERE vs HAVING
• Remarks
– WHERE vs HAVING
• WHERE – works on individual rows
• HAVING – works on groups / conditions on aggregation functions
– Statistical functions can only be used in SELECT, HAVING, ORDER
BY - not in WHERE, GROUP BY
– If statistical functions appear in the SELECT, then all items in the
SELECT-list have to be either statistical functions or group
identifications
SELECT CategoryID, MIN(UnitPrice) As Minimum
FROM Products
SQL: basic concepts revisited – Group by
Exercises
-- 1. Count the amount of products (columnname 'amount of products'), AND the amount of products in stock (=
unitsinstock not empty) (columnname 'Units in stock')
-- 2. How many employees have a function of Sales Representative (columnname 'Number of Sales
Representative')?
-- 3. Give the date of birth of the youngest employee (columnname 'Birthdate youngest') and the oldest
(columnname 'Birthdate oldest').
-- 4. What's the number of employees who will retire (at 65) within the first 20 years?
-- 5. Show a list of different countries where 2 of more suppliers are from. Order alphabeticaly.
-- 6. Which suppliers offer at least 5 products with a price less than 100 dollar? Show supplierId and the
number of different products.
-- The supplier with the highest number of products comes first.
SQL: basic concepts revisited
JOIN
• Select columns from several tables
– JOIN keyword : specifies which tables have to be joined and how
• Inner join
• Outer join
• Cross join
• ON keyword : specifies the JOIN condition
– Produces 1 result set, joining the rows of both tables
– Basic form (ANSI JOIN (SQL-92) <-> Old style join)
INNER JOIN
• Joins rows from one table with rows from another table
based on common criteria in the corresponding tables.
• The relation between the fields in the corresponding
tables is expressed through:
– = (equi-join)
– <
– >
– <>
– >=
SQL: basic concepts revisited – Join
INNER JOIN
• Example of equi-join
– Give the productID, productName and CategoryName for each
product
• ANSI JOIN (SQL-92)
SELECT ProductID, ProductName, CategoryName
FROM Products JOIN Categories
ON [Link] = [Link]
Aliases
• USE tables aliasses (via 'AS' or blank)
– SQL-92
SELECT ProductID, ProductName, CategoryName
FROM Products p JOIN Categories c
ON [Link] = [Link]
Remarks
• If the same column name is used in several tables in a query, then
each column name has to be preceeded by the table name or its
alias.
• Inner joins only return rows that meet the ON condition.
• If you omit (forget) the where clause in the old style join all
combinations are returned
= CROSS JOIN (= carthesian product) (see further)
SQL: basic concepts revisited – Join
OUTER JOIN
• Returns all records from 1 table, even if there is no
corresponding record in the other table
• 3 types of an outer join
– LEFT OUTER JOIN
• Returns all rows of the first table in the FROM clause(SQL-92)
– RIGHT OUTER JOIN
• Returns all rows of the second table in the FROM clause(SQL-92)
– FULL OUTER JOIN
• Returns all rows of the first and the second table in the FROM clause(SQL-92)
even if there is no corresponding record in the other table
SQL: basic concepts revisited – Join
OUTER JOIN
SQL: basic concepts revisited – Join
CROSS JOIN
• In a cross join the number of rows in the result set equals
the number of rows in the first table multiplied by the
number of rows in the second table
• Application: Generate all combinations
• Example: Make a schedule in which each employee
should contact each customer
SELECT [Link], [Link] + ' ' + [Link], [Link],
[Link], [Link], [Link], [Link]
FROM Employees e CROSS JOIN Customers c
SQL: basic concepts revisited
SET OPERATORS:
UNION – INTERSECT -
EXCEPT
SQL: basic concepts revisited - Union
UNION
• A UNION combines the result of 2 or more queries
– Basic form SELECT ... FROM ... WHERE ...
UNION
SELECT ... FROM ... WHERE ...
ORDER BY ...
– Rules
• Both SELECTs have to contain an equal number of columns
• Corresponding columns from both SELECTs should have compatible data types
• The columns names or aliases from the first SELECT or shown
• The result set does not contain duplicates. To keep duplicates use UNION ALL
• At the end an ORDER BY can be added.
Column names or expressions can't be used in the ORDER BY if they differ
between the two SELECTs. In this case use column numbers for sorting.
SQL: basic concepts revisited - Union
UNION
• Example: Give an overview of all employees (lastname
and firstname, city and postal code) and all customers
(name, city and postal code)
SELECT LastName + ' ' + FirstName as Name, City, Postalcode
FROM Employees
UNION
SELECT CompanyName, City, Postalcode
FROM Customers
SQL: basic concepts revisited - Intersect
INTERSECT
• Which records are in the intersection?
SELECT City, Country FROM Customers
INTERSECT
SELECT City, Country FROM Suppliers
SQL: basic concepts revisited - Except
EXCEPT
• The EXCEPT operator subtracts a result set from another
result set.
– Example: Which products have never been ordered?
SELECT ProductID
FROM Products
EXCEPT
SELECT ProductID
FROM OrderDetails
SQL: basic concepts revisited
Exercises
-- 1. Which suppliers (SupplierID and CompanyName) deliver Dairy Products?
-- 2. Give for each supplier the number of orders that contain products of that supplier.
-- Show supplierID, companyname and the number of orders.
-- Order by companyname.
-- 3. What’s for each category the lowest UnitPrice? Show category name and unit price.
-- 4. Give for each ordered product: productname, the least (columnname 'Min amount ordered') and the most
ordered (columnname 'Max amount ordered'). Order by productname.
-- 5. Give a summary for each employee with orderID, employeeID and employeename.
-- Make sure that the list also contains employees who don’t have orders yet.
SQL: basic concepts revisited
Exercises – Solutions
-- 1. Which suppliers (SupplierID and CompanyName) deliver Dairy Products?
SELECT DISTINCT [Link], [Link]
FROM Suppliers s JOIN Products p ON [Link] = [Link]
JOIN Categories c ON [Link] = [Link]
WHERE [Link] LIKE '%Dairy%'
-- 2. Give for each supplier the number of orders that contain products of that supplier.
-- Show supplierID, companyname and the number of orders.
-- Order by companyname.
select [Link], [Link], count(DISTINCT [Link]) As NrOfOrders
from Suppliers s join Products p ON [Link] = [Link]
JOIN OrderDetails od ON [Link] = [Link]
GROUP BY [Link], [Link]
ORDER BY [Link]
-- 3. What’s for each category the lowest UnitPrice? Show category name and unit price.
SELECT [Link], MIN([Link]) As 'Minimum UnitPrice'
FROM Products p join Categories c ON [Link] = [Link]
GROUP BY [Link]
SQL: basic concepts revisited
Exercises – Solutions
-- 4. Give for each ordered product: productname, the least (columnname 'Min amount ordered') and the most
ordered (columnname 'Max amount ordered'). Order by productname.
SELECT [Link], MIN([Link]) As 'Min amount ordered', Max([Link]) As 'Max amount ordered'
FROM Products p join OrderDetails od ON [Link] = [Link]
GROUP BY [Link]
ORDER BY [Link]
-- 5. Give a summary for each employee with orderID, employeeID and employeename.
-- Make sure that the list also contains employees who don’t have orders yet.
SELECT [Link], [Link] + ' ' + [Link] As 'Name', [Link]
FROM Employees e left join Orders o on [Link] = [Link]