SQL Revision
SQL Revision
Overview
• SQL stands for Structured Query Language
• SQL is a standard language for storing, manipulating and retrieving data in databases.
• Although SQL is an ANSI/ISO standard, there are different versions of the SQL language.
• However, to be compliant with the ANSI standard, they all support at least the major commands (such
as SELECT, UPDATE, DELETE, INSERT, WHERE) in a similar manner.
• RDBMS is the basis for SQL, and for all modern database systems such as MS SQL Server, IBM
DB2, Oracle, MySQL, and Microsoft Access.
• The data in RDBMS is stored in database objects called tables. A table is a collection of related data
entries and it consists of columns and rows.
• SQL keywords are NOT case sensitive: select is the same as SELECT
• Some database systems require a semicolon at the end of each SQL statement
• Semicolon is the standard way to separate each SQL statement in database systems that allow more
than one SQL statement to be executed in the same call to the server.
Some of The Most Important
SQL Commands
• SELECT - extracts data from a database
• UPDATE - updates data in a database
• DELETE - deletes data from a database
• INSERT INTO - inserts new data into a database
• CREATE DATABASE - creates a new database
• ALTER DATABASE - modifies a database
• CREATE TABLE - creates a new table
• ALTER TABLE - modifies a table
• DROP TABLE - deletes a table
• CREATE INDEX - creates an index (search key)
• DROP INDEX - deletes an index
SELECT DISTINCT
• Inside a table, a column often contains many duplicate values; and sometimes you only want to
list the different (distinct) values in that column
• Syntax - SELECT DISTINCT Country FROM Customers;
• Count Distinct: By using the DISTINCT keyword in a function called COUNT, we can return the
number of different unique values in that column
• Syntax - SELECT COUNT(DISTINCT Country) FROM Customers;
Operators in The WHERE Clause
1. = Equal
2. > Greater than
3. < Less than
4. >= Greater than or equal
5. <= Less than or equal
6. <> Not equal. Note: In some versions of SQL this operator may be written as !=
7. BETWEEN Between a certain range
8. LIKE Search for a pattern
9. IN To specify multiple possible values for a column
ORDER BY
• The ORDER BY keyword is used to sort the result-set in ascending or descending order
• The ORDER BY keyword sorts the records in ascending order by default. To sort the records in
descending order, use the DESC keyword.
• Syntax – Default sorting in
ascending order
SELECT * FROM Products
ORDER BY Price;
Using DESC to
sort in descending
SELECT * FROM Products order
ORDER BY Price DESC;
• For string values the ORDER BY keyword will order alphabetically and to sort the table reverse
alphabetically, use the DESC keyword:
SELECT * FROM Products
ORDER BY ProductName;
• Without parenthesis, the select statement will return all customers from Spain that starts with a "G", plus all
customers that starts with an "R", regardless of the country value:
• Select all customers that either are from Spain and starts with either "G", or starts with the letter "R":
UPDATE Customers
SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1;
• UPDATE Multiple Records: It is the WHERE clause that determines how many records will be
updated. The following SQL statement will update the ContactName to "Juan" for all records
where country is "Mexico":
UPDATE Customers
SET ContactName='Juan'
WHERE Country='Mexico';
DELETE Statement
• The DELETE statement is used to delete existing records in a table.
• Syntax
• DELETE FROM table_name WHERE condition;
• Note: Be careful when deleting records in a table! Notice the WHERE clause in the DELETE
statement. The WHERE clause specifies which record(s) should be deleted. If you omit the WHERE
clause, all records in the table will be deleted!
• Delete All Records: It is possible to delete all rows in a table without deleting the table. This
means that the table structure, attributes, and indexes will be intact:
DELETE FROM table_name;
The following SQL statement deletes all rows in the "Customers" table, without deleting the table:
DELETE FROM Customers;
• Delete a Table: To delete the table completely, use the DROP TABLE statement:
DROP TABLE Customers;
TOP, LIMIT, FETCH FIRST or
ROWNUM Clause
• The SELECT TOP clause is used to specify the number of records to return. The SELECT TOP clause is useful on
large tables with thousands of records. Returning a large number of records can impact performance.
• Not all database systems support the SELECT TOP clause. MySQL supports the LIMIT clause to select a limited
number of records, while Oracle uses FETCH FIRST n ROWS ONLY and ROWNUM.
• My SQL Synatx:
SELECT column_name(s)
FROM table_name
WHERE condition
LIMIT number;
SELECT MAX(Price)
FROM Products;
• Set Column Name (Alias):
• SELECT MIN(Price) AS SmallestPrice
FROM Products;
• Use MIN() with GROUP BY: Here we use the MIN() function and the GROUP BY clause, to return
the smallest price for each category in the Products table:
SELECT MIN(Price) AS SmallestPrice, CategoryID
FROM Products
GROUP BY CategoryID;
COUNT() Function
• The COUNT() function returns the number of rows that matches a specified criterion
• Find the total number of rows in the Products table:
SELECT COUNT(*)
FROM Products;
• You can specify a column name instead of the asterix symbol (*). If you specify a column name instead of
(*), NULL values will not be counted.
SELECT COUNT(ProductName)
FROM Products;
• Ignore Duplicates: You can ignore duplicates by using the DISTINCT keyword in the COUNT() function. If
DISTINCT is specified, rows with the same value for the specified column will be counted as one.
SELECT COUNT(DISTINCT Price)
FROM Products;
• Use an Alias: Give the counted column a name by using the AS keyword.
• Use COUNT() with GROUP BY: Here we use the COUNT() function and the GROUP BY clause, to return
the number of records for each category in the Products table:
SELECT COUNT(*) AS [Number of records], CategoryID
FROM Products
GROUP BY CategoryID;
SUM() Function
• The SUM() function returns the total sum of a numeric column.
• Add a WHERE Clause: You can add a WHERE clause to specify conditions:
SELECT SUM(Quantity)
FROM OrderDetails
WHERE ProductId = 11;
• Use SUM() with GROUP BY: ere we use the SUM() function and the GROUP BY clause, to return the Quantity for
each OrderID in the OrderDetails table:
SELECT OrderID, SUM(Quantity) AS [Total Quantity]
FROM OrderDetails
GROUP BY OrderID;
• SUM() With an Expression: The parameter inside the SUM() function can also be an expression. If we assume that
each product in the OrderDetails column costs 10 dollars, we can find the total earnings in dollars by multiply each
quantity with 10:
SELECT SUM(Quantity * 10)
FROM OrderDetails;
We can also join the OrderDetails table to the Products table to find the actual amount, instead of assuming it is 10
dollars:
SELECT SUM(Price * Quantity)
FROM OrderDetails
LEFT JOIN Products ON OrderDetails.ProductID = Products.ProductID;
AVG() Function
• The AVG() function returns the average value of a numeric column. Note: NULL values are ignored.
SELECT AVG(Price)
FROM Products;
• Add a WHERE Clause:
SELECT AVG(Price)
FROM Products
WHERE CategoryID = 1;
• Higher Than Average: To list all records with a higher price than average, we can use the AVG()
function in a sub query:
• Return all products with a higher price than the average price:
SELECT * FROM Products
WHERE price > (SELECT AVG(price) FROM Products);
• Use AVG() with GROUP BY: Here we use the AVG() function and the GROUP BY clause, to return
the average price for each category in the Products table:
SELECT AVG(Price) AS AveragePrice, CategoryID
FROM Products
GROUP BY CategoryID;
LIKE Operator
• The LIKE operator is used in a WHERE clause to search for a specified pattern in a column
• There are two wildcards often used in conjunction with the LIKE operator:
• The percent sign % represents zero, one, or multiple characters
• The underscore sign _ represents one, single character
• Syntax:
• SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;
• The _ Wildcard: The _ wildcard represents a single character. It can be any character or number, but each _
represents one, and only one, character.
• E.g., Return all customers from a city that starts with 'L' followed by one wildcard character, then 'nd' and then two
wildcard characters:
SELECT * FROM Customers
WHERE city LIKE 'L_nd__’;
• The % Wildcard: The % wildcard represents any number of characters, even zero characters.
• Starts With: To return records that starts with a specific letter or phrase, add the % at the end of the letter or phrase
SELECT * FROM Customers
WHERE CustomerName LIKE 'La%’;
• Ends With: To return records that ends with a specific letter or phrase, add the % at the beginning of the letter or phrase.
SELECT * FROM Customers
WHERE CustomerName LIKE '%a’;
• You can also combine "starts with" and "ends with":
SELECT * FROM Customers
WHERE CustomerName LIKE 'b%s’;
• Contains: To return records that contains a specific letter or phrase, add the % both before and after the letter or phrase.
• SELECT * FROM Customers
WHERE CustomerName LIKE '%or%’;
• Combine Wildcards: Any wildcard, like % and _ , can be used in combination with other wildcards.
• Return all customers that starts with "a" and are at least 3 characters in length:
SELECT * FROM Customers
WHERE CustomerName LIKE 'a__%’;
• Return all customers that have "r" in the second position:
SELECT * FROM Customers
WHERE CustomerName LIKE '_r%’;
• Without Wildcard: If no wildcard is specified, the phrase has to have an exact match to return a result.
• Return all customers from Spain:
SELECT * FROM Customers
WHERE Country LIKE 'Spain’;
• NOT LIKE:
SELECT * FROM Customers
WHERE Country NOT LIKE 'Spain’;
Wildcards
• A wildcard character is used to substitute one or more characters in a string.
• Wildcard characters are used with the LIKE operator. The LIKE operator is used in a WHERE clause
to search for a specified pattern in a column.
• Wildcard Characters
% Represents zero or more characters
_ Represents a single character
[] Represents any single character within the brackets *
^ Represents any character not in the brackets *
- Represents any single character within the specified range *
{} Represents any escaped character **
• We will join the Products table with the Categories table, by using the CategoryID field from both tables:
SELECT ProductID, ProductName, CategoryName
FROM Products
INNER JOIN Categories ON Products.CategoryID = Categories.CategoryID;
Note: The INNER JOIN keyword returns only rows with a match in both tables. Which means that if you have a
product with no CategoryID, or with a CategoryID that is not present in the Categories table, that record would
not be returned in the result.
INNER JOIN (2/2)
• Naming the Columns: It is a good practice to include the table name when specifying columns in
the SQL statement:
SELECT Products.ProductID, Products.ProductName,
Categories.CategoryName
FROM Products
INNER JOIN Categories ON Products.CategoryID = Categories.CategoryID;
Note: The example above works without specifying table names, because none of the specified column
names are present in both tables. If you try to include CategoryID in the SELECT statement, you will get an
error if you do not specify the table name (because CategoryID is present in both tables).
• JOIN or INNER JOIN: JOIN and INNER JOIN will return the same result. INNER is the default join
type for JOIN, so when you write JOIN the parser actually writes INNER JOIN.
• JOIN Three Tables: The following SQL statement selects all orders with customer and shipper
information:
SELECT Orders.OrderID, Customers.CustomerName, Shippers.ShipperName
FROM ((Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID)
INNER JOIN Shippers ON Orders.ShipperID = Shippers.ShipperID);
LEFT JOIN
• The LEFT JOIN keyword returns all records from the left table (table1), and the matching records
from the right table (table2). The result is 0 records from the right side, if there is no match.
• Syntax:
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
• The following SQL statement will select all customers, and any orders they might have:
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID
ORDER BY Customers.CustomerName;
Note: The LEFT JOIN keyword returns all records from the left table (Customers), even if there are no
matches in the right table (Orders).
RIGHT JOIN
• The RIGHT JOIN keyword returns all records from the right table (table2), and the matching
records from the left table (table1). The result is 0 records from the left side, if there is no match.
• Syntax:
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
• The following SQL statement will return all employees, and any orders they might have placed:
SELECT Orders.OrderID, Employees.LastName, Employees.FirstName
FROM Orders
RIGHT JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID
ORDER BY Orders.OrderID;
Note: The RIGHT JOIN keyword returns all records from the right table (Employees), even if there are no
matches in the left table (Orders).
FULL OUTER JOIN
• The FULL OUTER JOIN keyword returns all records when there is a match in left (table1) or right (table2) table records.
FULL OUTER JOIN and FULL JOIN are the same.
• Syntax:
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;
• The following SQL statement selects all customers, and all orders:
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
FULL OUTER JOIN Orders ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;
Note: The FULL OUTER JOIN keyword returns all matching records from both tables whether the other table matches or not. So, if
there are rows in "Customers" that do not have matches in "Orders", or if there are rows in "Orders" that do not have matches in
"Customers", those rows will be listed as well.
• FULL JOIN is not supported in MySQL, you have to use combination of LEFT JOIN & RIGHT JOIN with UNION to get FULL
JOIN in MySQL:
SELECT * FROM table1
LEFT JOIN table2 ON table1.column = table2.column
UNION
SELECT * FROM table1
RIGHT JOIN table2 ON table1.column = table2.column;
Self Join
• A self join is a regular join, but the table is joined with itself.
• Syntax:
SELECT column_name(s)
FROM table1 T1, table1 T2
WHERE condition;
*T1 and T2 are different table aliases for the same table.
• The following SQL statement matches customers that are from the same city:
SELECT A.CustomerName AS CustomerName1,
B.CustomerName AS CustomerName2, A.City
FROM Customers A, Customers B
WHERE A.CustomerID <> B.CustomerID
AND A.City = B.City
ORDER BY A.City;
UNION (1/2)
• The UNION operator is used to combine the result-set of two or more SELECT statements.
• Every SELECT statement within UNION must have the same number of columns
• The columns must also have similar data types
• The columns in every SELECT statement must also be in the same order
• Syntax:
SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;
• UNION ALL Syntax: The UNION operator selects only distinct values by default. To allow duplicate values, use UNION ALL:
SELECT column_name(s) FROM table1
UNION ALL
SELECT column_name(s) FROM table2;
Note: The column names in the result-set are usually equal to the column names in the first SELECT statement.
• The following SQL statement returns the cities (only distinct values) from both the "Customers" and the "Suppliers" table:
SELECT City FROM Customers
UNION
SELECT City FROM Suppliers
ORDER BY City;
Note: If some customers or suppliers have the same city, each city will only be listed once, because UNION selects only distinct
values. Use UNION ALL to also select duplicate values!
UNION (2/2)
• The following SQL statement returns the cities (duplicate values also) from both the "Customers" and the "Suppliers" table:
SELECT City FROM Customers
UNION ALL
SELECT City FROM Suppliers
ORDER BY City;
• SQL UNION With WHERE: The following SQL statement returns the German cities (only distinct values) from both the
"Customers" and the "Suppliers" table:
SELECT City, Country FROM Customers
WHERE Country='Germany'
UNION
SELECT City, Country FROM Suppliers
WHERE Country='Germany'
ORDER BY City;
Note:Use UNION ALL to returns the German cities (duplicate values also) from both the "Customers" and the "Suppliers“
• The following SQL statement lists all customers and suppliers:
SELECT 'Customer' AS Type, ContactName, City, Country
FROM Customers
UNION
SELECT 'Supplier', ContactName, City, Country
FROM Suppliers;
Notice the "AS Type" above - it is an alias. SQL Aliases are used to give a table or a column a temporary name. An alias only exists for
the duration of the query. So, here we have created a temporary column named "Type", that list whether the contact person is a
"Customer" or a "Supplier".
GROUP BY
• The GROUP BY statement groups rows that have the same values into summary rows, like "find the number
of customers in each country
• The GROUP BY statement is often used with aggregate functions (COUNT(), MAX(), MIN(), SUM(), AVG()) to
group the result-set by one or more columns
• E.g: The following SQL statement lists the number of customers in each country:
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country;
The following SQL statement lists the number of customers in each country, sorted high to low:
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
ORDER BY COUNT(CustomerID) DESC;
• GROUP BY With JOIN Example: The following SQL statement lists the number of orders sent by each
shipper:
SELECT Shippers.ShipperName, COUNT(Orders.OrderID) AS NumberOfOrders FROM Order
s
LEFT JOIN Shippers ON Orders.ShipperID = Shippers.ShipperID
GROUP BY ShipperName;
HAVING Clause
• The HAVING clause was added to SQL because the WHERE keyword cannot be used with aggregate functions
• Syntax:
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
E.g.1 The following SQL statement lists the number of customers in each country. Only include countries with more than 5 customers:
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5;
E.g.2: The following SQL statement lists the number of customers in each country, sorted
high to low (Only include countries with more than 5 customers):
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5
ORDER BY COUNT(CustomerID) DESC;
E.g.3: The following SQL statement lists if the employees "Davolio" or "Fuller" have registered more than 25 orders:
SELECT Employees.LastName, COUNT(Orders.OrderID) AS NumberOfOrders
FROM Orders
INNER JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID
WHERE LastName = 'Davolio' OR LastName = 'Fuller'
EXISTS Operator
• The EXISTS operator is used to test for the existence of any record in a subquery. The EXISTS operator
returns TRUE if the subquery returns one or more records.
• Syntax:
• SELECT column_name(s)
FROM table_name
WHERE EXISTS
(SELECT column_name FROM table_name WHERE condition);
• Examples:
• The following SQL statement returns TRUE and lists the suppliers with a product price less than 20:
SELECT SupplierName
FROM Suppliers
WHERE EXISTS (SELECT ProductName FROM Products WHERE Products.SupplierID =
Suppliers.supplierID AND Price < 20);
• The following SQL statement returns TRUE and lists the suppliers with a product price equal to 22:
SELECT SupplierName
FROM Suppliers
WHERE EXISTS (SELECT ProductName FROM Products WHERE Products.SupplierID =
Suppliers.supplierID AND Price = 22);
ANY and ALL Operators (1/2)
• The ANY and ALL operators allow you to perform a comparison between a single column value and a range of other
values.
• ANY: The ANY operator:
• returns a boolean value as a result
• returns TRUE if ANY of the subquery values meet the condition
• ANY means that the condition will be true if the operation is true for any of the values in the range.
• ANY Syntax:
SELECT column_name(s)
FROM table_name
WHERE column_name operator ANY
(SELECT column_name
FROM table_name
WHERE condition);
Note: The operator must be a standard comparison operator (=, <>, !=, >, >=, <, or <=).
E.g., The following SQL statement lists the ProductName if it finds ANY records in the OrderDetails table has Quantity equal to 10
(this will return TRUE because the Quantity column has some values of 10):
SELECT ProductName
FROM Products
WHERE ProductID = ANY
(SELECT ProductID
FROM OrderDetails
WHERE Quantity = 10);
ANY and ALL Operators (2/2)
• The ALL operator:
• returns a boolean value as a result
• returns TRUE if ALL of the subquery values meet the condition
• is used with SELECT, WHERE and HAVING statements
ALL means that the condition will be true only if the operation is true for all values in the range.
• ALL Syntax With SELECT:
• SELECT ALL column_name(s)
FROM table_name
WHERE condition;
• ALL Syntax With WHERE or HAVING:
• SELECT column_name(s)
FROM table_name
WHERE column_name operator ALL
(SELECT column_name
FROM table_name
WHERE condition);
Note: The operator must be a standard comparison operator (=, <>, !=, >, >=, <, or <=).
E.g.1 The following SQL statement lists ALL the product names:
SELECT ALL ProductName
FROM Products
WHERE TRUE;
E.g.2: The following SQL statement lists the ProductName if ALL the records in the OrderDetails table has Quantity equal to 10. This will of course return
FALSE because the Quantity column has many different values (not only the value of 10):
SELECT ProductName
FROM Products
WHERE ProductID = ALL
(SELECT ProductID
FROM OrderDetails
SELECT INTO
• The SELECT INTO statement copies data from one table into a new table. However, it is not supported in MySQL
• In MySQL, you can use create table in combination with select as an alternative for select into
E.g.,
create table HyderabadPeople
select * from people where location = 'Hyderabad’;
• It copies all rows/columns of people table into HyderabadPeople table where their location is mentioned as
Hyderabad in people table
• You can also copy specific columns:
• E.g.
create table YummiesTeam
select Salesperson, Team from people where Team = 'Yummies’;
It copies only Salesperson name from Yummies’ team from people table
• The following SQL statement copies data from more than one table into a new table:
create table mergedtable
select s.SPID, p.salesperson, s.GeoID, s.PID, s.SaleDate, s.Amount from sales s
left join people p on p.SPID = s.SPID;
It extracts salesperson column from people table and rest columns from sales table in the new ‘mergedtable’