Week-8
1.1 Filtering data: WHERE, AND, OR
The WHERE clause is used to filter records. The WHERE clause is used to extract only those
records that fulfill a specified condition. The syntax of SELECT after incorporating WHERE
clause.
SELECT <Attribute List>
FROM <Table List>
WHERE <Condition>;
Where <Condition> is a conditional expression that identifies the tuples(rows) to be
retrieved by the Query.
Example 1: This query involves only the EMPLOYEE relation listed in the FROM clause. The
query selects the individual EMPLOYEE tuples that satisfy the condition of the WHERE clause, then
projects the result on the Bdate and Address attributes listed in the SELECT clause.
SELECT Bdate,Address
FROM Employee
WHERE ssn=121;
Operators in The WHERE Clause are
Operator Description
= Equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
<> Not equal
BETWEEN Between a certain range
LIKE Search for a pattern
IN To specify multiple possible values for a column
AND
The AND operator allows the existence of multiple conditions in a SQL statement's WHERE
clause. While using AND operator, complete condition will be assumed true when all the
conditions are true.
Consider the table EMPLOYEE having records as follows –
EMPLOYEE
id | name | age | address | salary
----+-------+-----+-----------+--------
1 | Paul | 32 | California| 20000
2 | Allen | 25 | Texas | 15000
3 | Teddy | 23 | Norway | 20000
4 | Mark | 25 | Rich-Mond | 65000
5 | David | 27 | Texas | 85000
6 | Kim | 22 | South-Hall| 45000
7 | James | 24 | Houston | 10000
Query 0. Retrieve the details of all employees where AGE is greater than or equal to
25 AND salary is greater than or equal to 65000.
SELECT * FROM EMPLOYEE WHERE AGE >= 25 AND SALARY >= 65000;
Only those tuples that satisfy the condition—that is, those tuples for which the condition
evaluates to TRUE after substituting their corresponding attribute values—are selected
Output
id | name | age | address | salary
----+-------+-----+------------+--------
4 | Mark | 25 | Rich-Mond | 65000
5 | David | 27 | Texas | 85000
(2 rows)
OR
The OR operator is also used to combine multiple conditions in a SQL statement's WHERE
clause. While using OR operator, complete condition will be assumed true when at least any of
the conditions is true. For example [condition1] OR [condition2] will be true if either condition1
or condition2 is true.
Syntax
The basic syntax of OR operator with WHERE clause is as follows −
SELECT column1, column2, columnN
FROM table_name
WHERE [condition1] OR [condition2]...OR [conditionN]
The following SELECT statement lists down all the records where AGE is greater than or equal
to 25 OR salary is greater than or equal to 65000.00 −
SELECT * FROM EMPLOYEE WHERE AGE >= 25 OR SALARY >= 65000;
The above given SQL statement will produce the following result
Output
id | name | age | address | salary
----+-------+-----+------------+--------
1 | Paul | 32 | California | 20000
2 | Allen | 25 | Texas | 15000
4 | Mark | 25 | Rich-Mond | 65000
5 | David | 27 | Texas | 85000
(4 rows)
1.2 Row limiting clause: This clause allows sql queries to limit the number of rows returned
and to specify a starting row for the return set.
Syntax:
SELECT column_name(s)
FROM table_name
ORDER BY column_name(s)
FETCH FIRST number ROWS ONLY;
Example:
The following SQL statement selects the first three records from the "Customers" table
SELECT * FROM Customers
FETCH FIRST 3 ROWS ONLY;
1.3 Filtering data:IN, BETWEEN, LIKE
IN
IN operator in the WHERE clause to check if a value matches any value in a list of values.
The syntax of the IN operator is as follows:
SELECT * FROM TABLE_NAME WHERE value IN (value1,value2,...)
The IN operator returns true if the value matches any value in the list i.e., value1 , value2 ,.The list
of values can be a list of literal values such as numbers, strings or a result of a SELECT statement
like this:
SELECT * FROM TABLE_NAME WHERE value IN (SELECT column_name FROM
table_name);
Example:
SELECT * FROM EMPLOYEE WHERE ID IN (1,2,3);
The above Query display output as follows.
id | name | age | address | salary
----+-------+-----+-----------+--------
1 | Paul | 32 | California| 20000
2 | Allen | 25 | Texas | 15000
3 | Teddy | 23 | Norway | 20000
BETWEEN
We use the BETWEEN operator to match a value against a range of values. The following illustrates
the syntax of the BETWEEN operator:
SELECT * FROM table_name WHERE value BETWEEN low AND high;
If the value is greater than or equal to the low value and less than or equal to the high value, the
expression returns true, otherwise, it returns false.
You can rewrite the BETWEEN operator by using the greater than or equal ( >=) or less than or equal
( <=) operators like this:
SELECT * FROM table_name WHERE value >= low and value <= high;
Example:
SELECT * FROM EMPLOYEE WHERE salary BETWEEN 20000 and 75000;
The above Query display output as follows
id | name | age | address | salary
----+-------+-----+-----------+--------
1 | Paul | 32 | California| 20000
3 | Teddy | 23 | Norway | 20000
4 | Mark | 25 | Rich-Mond | 65000
6 | Kim | 22 | South-Hall| 45000
LIKE
The LIKE operator is used to match text values against a pattern using wildcards. If the search
expression can be matched to the pattern expression, the LIKE operator will return true, which is 1.
There are two wildcards used in conjunction with the LIKE operator −
➢ The percent sign (%)
➢ The underscore (_)
The percent sign represents zero, one, or multiple numbers or characters. The underscore represents a
single number or character. These symbols can be used in combinations.
If either of these two signs is not used in conjunction with the LIKE clause, then the LIKE acts like
the equals operator.
Syntax
The basic syntax of % and _ is as follows −
SELECT FROM table_name WHERE column LIKE '%XXXX%
And
SELECT FROM table_name WHERE column LIKE '_XXXX
You can combine any number of conditions using AND or OR operators. Here XXXX could be any numeric
or string value.
For Example 1 :
SELECT * FROM EMPLOYEE WHERE AGE::text LIKE '2%'
This would produce the following result
id | name | age | address | salary
----+-------+-----+-------------+--------
2 | Allen | 25 | Texas | 15000
3 | Teddy | 23 | Norway | 20000
4 | Mark | 25 | Rich-Mond | 65000
5 | David | 27 | Texas | 85000
6 | Kim | 22 | South-Hall | 45000
7 | James | 24 | Houston | 10000
8 | Paul | 24 | Houston | 20000
(7 rows)
Which would display all the records from EMPLOYEE table where AGE starts with 2?
Example 2:
SELECT * FROM EMPLOYEE WHERE ADDRESS LIKE '%-%';
This would produce the following result
1.4 Join operation on tables in SQL:
A JOIN clause is used to combine rows from two or more tables, based on a related column
between them.
Supported Types of Joins in MySQL
• INNER JOIN: Returns records that have matching values in both tables
• LEFT JOIN: Returns all records from the left table, and the matched records from the right
table
• RIGHT JOIN: Returns all records from the right table, and the matched records from the left
table
• CROSS JOIN: Returns all records from both tables
1. INNER JOIN
The INNER JOIN keyword selects records that have matching values in both tables.
INNER JOIN Syntax:
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
Order Table:
OrderID CustomerID EmployeeID OrderDate ShipperID
10308 2 7 1996-09-18 3
10309 37 3 1996-09-19 1
10310 77 8 1996-09-20 2
Customers Table:
CustomerID CustomerName ContactName Address City PostalCode Country
1 Alfreds Maria Anders Obere Str. Berlin 12209 Germany
Futterkiste 57
2 Ana Trujillo Ana Trujillo Avda. de la México 05021 Mexico
Emparedados y Constitución D.F.
helados 2222
3 Antonio Moreno Antonio Mataderos México 05023 Mexico
Taquería Moreno 2312 D.F.
MySQL INNER JOIN Example
The following SQL statement selects all orders with customer information:
SELECT Orders.OrderID, Customers.CustomerName
FROM Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;
The INNER JOIN keyword selects all rows from both tables as long as there is a match between the
columns. If there are records in the "Orders" table that do not have matches in "Customers", these
orders will not be shown!
2. LEFT JOIN
The LEFT JOIN keyword returns all records from the left table (table1), and the
matching records (if any) from the right table (table2).
Syntax:
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
Example: 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).
3. RIGHT JOIN
The RIGHT JOIN keyword returns all records from the right table (table2), and the matching records (if
any) from the left table (table1)
Syntax:
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
Example:
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 RIGHT JOIN keyword returns all records from the right table (Employees), even if there are
no matches in the left table (Orders).
4. CROSS JOIN
The CROSS JOIN keyword returns all records from both tables (table1 and table2).
Syntax:
SELECT column_name(s)
FROM table1
CROSS JOIN table2;
Example: The following SQL statement selects all customers, and all orders:
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
CROSS JOIN Orders;