0% found this document useful (0 votes)
8 views

SQL Revision

The document provides a comprehensive overview of SQL, detailing its purpose as a standard language for database management and the various commands used for data manipulation, such as SELECT, UPDATE, DELETE, and INSERT. It explains key concepts like NULL values, aggregate functions, and the use of operators in SQL queries, including the LIKE operator and wildcards. Additionally, it covers the importance of the WHERE clause and the implications of omitting it in commands that modify or delete records.

Uploaded by

svishwash57
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

SQL Revision

The document provides a comprehensive overview of SQL, detailing its purpose as a standard language for database management and the various commands used for data manipulation, such as SELECT, UPDATE, DELETE, and INSERT. It explains key concepts like NULL values, aggregate functions, and the use of operators in SQL queries, including the LIKE operator and wildcards. Additionally, it covers the importance of the WHERE clause and the implications of omitting it in commands that modify or delete records.

Uploaded by

svishwash57
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

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;

SELECT * FROM Products


ORDER BY ProductName DESC;
ORDER BY Several Columns
• The following SQL statement selects all customers from the "Customers" table, sorted by the
"Country" and the "CustomerName" column. This means that it orders by Country, but if some
rows have the same Country, it orders them by CustomerName:
• SELECT * FROM Customers
ORDER BY Country, CustomerName;
• Using Both ASC and DESC : The following SQL statement selects all customers from the
"Customers" table, sorted ascending by the "Country" and descending by the "CustomerName"
column:
• SELECT * FROM Customers
ORDER BY Country ASC, CustomerName DESC;
Combining AND and OR
• Select all Spanish customers that starts with either "G" or "R":
SELECT * FROM Customers
WHERE Country = 'Spain' AND (CustomerName LIKE 'G
%' OR CustomerName LIKE 'R%’);

• 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":

SELECT * FROM Customers


WHERE Country = 'Spain' AND CustomerName LIKE 'G
%' OR CustomerName LIKE 'R%';
The NOT Operator
• The NOT operator is used in combination with other operators to give the opposite result, also called
the negative result
• Select only the customers that are NOT from Spain:
SELECT * FROM Customers
WHERE NOT Country = 'Spain’;
• NOT LIKE: Select customers that does not start with the letter 'A’:
SELECT * FROM Customers
WHERE CustomerName NOT LIKE 'A%’;
• NOT BETWEEN: Select customers with a customerID not between 10 and 60:
SELECT * FROM Customers
WHERE CustomerID NOT BETWEEN 10 AND 60;
• NOT IN: Select customers that are not from Paris or London:
SELECT * FROM Customers
WHERE City NOT IN ('Paris', 'London’);
• NOT Greater Than: Select customers with a CustomerId not greater than 50:
SELECT * FROM Customers
WHERE NOT CustomerID > 50;
Note: There is a not-greater-then operator: !> that would give you the same result. Similarly, There is a not-
less-then operator: !<
INSERT INTO
• The INSERT INTO statement is used to insert new records in a table.
• It is possible to write the INSERT INTO statement in two ways:
• Specify both the column names and the values to be inserted:
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
• If you are adding values for all the columns of the table, you do not need to specify the column names in the SQL query. However,
make sure the order of the values is in the same order as the columns in the table. Here, the INSERT INTO syntax would be as
follows:
INSERT INTO table_name
VALUES (value1, value2, value3, ...);

INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)


VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway’);
• Insert Data Only in Specified Columns:
INSERT INTO Customers (CustomerName, City, Country)
VALUES ('Cardinal', 'Stavanger', 'Norway’);
• Insert Multiple Rows:
INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
VALUES
('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway'),
('Greasy Burger', 'Per Olsen', 'Gateveien 15', 'Sandnes', '4306', 'Norway'),
('Tasty Tee', 'Finn Egan', 'Streetroad 19B', 'Liverpool', 'L1 0AA', 'UK');
NULL Value
• A field with a NULL value is a field with no value i.e., A NULL value is different from a zero value or
a field that contains spaces. A field with a NULL value is one that has been left blank during record
creation!
• It is not possible to test for NULL values with comparison operators, such as =, <, or <>. We will
have to use the IS NULL and IS NOT NULL operators instead.
SELECT CustomerName, ContactName, Address
FROM Customers
WHERE Address IS NULL;

SELECT CustomerName, ContactName, Address


FROM Customers
WHERE Address IS NOT NULL;
UPDATE Statement
• The UPDATE statement is used to modify the existing records in a table.
• Be careful when updating records in a table! Notice the WHERE clause in the UPDATE statement.
The WHERE clause specifies which record(s) that should be updated. If you omit the WHERE
clause, all records in the table will be updated!
• Syntax:
• UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

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 * FROM Customers


LIMIT 3;

SELECT * FROM Customers


WHERE Country='Germany'
LIMIT 3;
• ADD the ORDER BY Keyword: Add the ORDER BY keyword when you want to sort the result, and return the first n
records of the sorted result.
SELECT * FROM Customers
ORDER BY CustomerName DESC
LIMIT 3;
Aggregate Functions
• An aggregate function is a function that performs a calculation on a set of values, and returns a
single value.
• Aggregate functions are often used with the GROUP BY clause of the SELECT statement. The
GROUP BY clause splits the result-set into groups of values and the aggregate function can be
used to return a single value for each group.
• The most commonly used SQL aggregate functions are:
• MIN() - returns the smallest value within the selected column
• MAX() - returns the largest value within the selected column
• COUNT() - returns the number of rows in a set
• SUM() - returns the total sum of a numerical column
• AVG() - returns the average value of a numerical column
• Aggregate functions ignore null values (except for COUNT()).
MIN() and MAX() Functions
• Syntax:
SELECT MIN(Price)
FROM Products;

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 **

*Not supported in PostgreSQL and MySQL databases.


** Supported only in Oracle databases.
IN Operator
• The IN operator allows you to specify multiple values in a WHERE clause. The IN operator is a
shorthand for multiple OR conditions.
• NOT IN: Return all customers that are NOT from 'Germany', 'France', or 'UK’
• IN (SELECT): You can also use IN with a subquery in the WHERE clause. With a subquery you can
return all records from the main query that are present in the result of the subquery
• Return all customers that have an order in the Orders table:
SELECT * FROM Customers
WHERE CustomerID IN (SELECT CustomerID FROM Orders);
• NOT IN (SELECT): Return all customers that have NOT placed any orders in the Orders table:
SELECT * FROM Customers
WHERE CustomerID NOT IN (SELECT CustomerID FROM Orders);
BETWEEN Operator
• The BETWEEN operator selects values within a given range. The values can be numbers, text, or dates.
• The BETWEEN operator is inclusive: begin and end values are included.
• Syntax:
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
• NOT BETWEEN:
SELECT * FROM Products
WHERE Price NOT BETWEEN 10 AND 20;
• BETWEEN with IN:
SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20
AND CategoryID IN (1,2,3);
• BETWEEN Text Values: The following SQL statement selects all products with a ProductName alphabetically between Carnarvon Tigers and
Mozzarella di Giovanni:
SELECT * FROM Products
WHERE ProductName BETWEEN 'Carnarvon Tigers' AND 'Mozzarella di Giovanni'
ORDER BY ProductName;
• NOT BETWEEN Text Values:
SELECT * FROM Products
WHERE ProductName NOT BETWEEN 'Carnarvon Tigers' AND 'Mozzarella di Giovanni'
ORDER BY ProductName;
• BETWEEN Dates:
SELECT * FROM Orders
WHERE OrderDate BETWEEN #07/01/1996# AND #07/31/1996#;
OR
SELECT * FROM Orders
WHERE OrderDate BETWEEN '1996-07-01' AND '1996-07-31';
SQL Aliases for Columns
• 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.
SELECT CustomerID AS ID
FROM Customers;
• AS is Optional: Actually, in most database languages, you can skip the AS keyword and get the same result:
SELECT CustomerID ID
FROM Customers;
• Using Aliases With a Space Character: If you want your alias to contain one or more spaces, like "My Great
Products", surround your alias with square brackets or double quotes
• SELECT ProductName AS [My Great Products]
FROM Products;
• SELECT ProductName AS "My Great Products"
FROM Products;
*[] is not supported in MySQL
• Concatenate Columns: The following SQL statement creates an alias named "Address" that combine four columns
(Address, PostalCode, City and Country):
SELECT CustomerName, CONCAT(Address,', ',PostalCode,', ',City,',
',Country) AS Address
FROM Customers;
SQL Aliases for Tables
• The same rules applies when you want to use an alias for a table.
• Refer to the Customers table as Persons instead:
SELECT * FROM Customers AS Persons;
• It might seem useless to use aliases on tables, but when you are using more than one table in
your queries, it can make the SQL statements shorter:
• The following SQL statement selects all the orders from the customer with CustomerID=4 (Around the
Horn). We use the "Customers" and "Orders" tables, and give them the table aliases of "c" and "o"
respectively (Here we use aliases to make the SQL shorter):
SELECT o.OrderID, o.OrderDate, c.CustomerName
FROM Customers AS c, Orders AS o
WHERE c.CustomerName='Around the Horn' AND c.CustomerID=o.CustomerID;
• Aliases can be useful when:
• There are more than one table involved in a query
• Functions are used in the query
• Column names are big or not very readable
• Two or more columns are combined together
JOINS
• A JOIN clause is used to combine rows from two or more tables, based on a related column
between them.
• Different Types of SQL JOINs:
• (INNER) JOIN: Returns records that have matching values in both tables
• LEFT (OUTER) JOIN: Returns all records from the left table, and the matched records from the right table
• RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched records from the left
table
• FULL (OUTER) JOIN: Returns all records when there is a match in either left or right table
INNER JOIN (1/2)
• The INNER JOIN keyword selects records that have matching values in both tables.
• Syntax:
• SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
• Let's look at a selection of thePrice
Products table and a selection of the Categories table:
ProductIDProductName CategoryID CategoryID CategoryName Description
1 Chais 1 18 1 Beverages Soft drinks, coffees, teas, beers, and ales
2 Chang 1 19 2 Condiments Sweet and savory sauces, relishes, spreads, and seasonings

3 Aniseed Syrup 2 10 3 Confections Desserts, candies, and sweet breads

• 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’

You might also like