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

Lecture Slide 3

ddadfds dds

Uploaded by

ayk0227
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Lecture Slide 3

ddadfds dds

Uploaded by

ayk0227
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

QUERYING DATA FROM

RELATIONAL DATABASES USING


SQL
OUTLIN
E

⯈ Where Clause Operators

⯈ Sorting Records

⯈ Joining Tables

⯈ Handling NULL values

⯈ Aggregate Functions
QUERYING (RETRIEVING) DATA FROM
RELATIONAL DATABASES
A condition in a where clause includes an attribute name, an operator, and attribute value.

Operators in The WHERE Clause


The following operators can be used in the WHERE clause:

Operator Description
= Equal
<> Not equal. Note: In some versions of SQL this operator may be written as !=

> Greater than


< Less than
>= Greater than or equal
<= Less than or equal
BETWEEN Between an inclusive range
LIKE Search for a pattern
IN To specify multiple possible values for a column
QUERYING (RETRIEVING) DATA FROM
RELATIONAL
The SQL LIKE Operator
DATABASES
The LIKE operator is used in a WHERE clause to search for a specified pattern in a
column.

There are two wildcards used in conjunction with the LIKE operator:
•% - The percent sign represents zero, one, or multiple characters
•_ - The underscore represents a single character

The percent sign and the underscore can also be used in combinations!

LIKE Syntax

SELECT column1, column2, ...


FROM table_name
WHERE columnN LIKE pattern;
QUERYING (RETRIEVING) DATA FROM
RELATIONAL DATABASES
Examples of using LIKE Operator

The following SQL statement selects all customers with a CustomerName starting with “b":
SELECT * FROM Customers
WHERE CustomerName LIKE ’b%’;

The following SQL statement selects all customers with a CustomerName ending with “b":

SELECT * FROM Customers


WHERE CustomerName LIKE ’%b’;

The following SQL statement selects all customers with a CustomerName that have "or" in any
position:
SELECT * FROM Customers
WHERE CustomerName LIKE '%or%';

The following SQL statement selects all customers with a CustomerName that have "r" in the
second position:
SELECT * FROM Customers
WHERE CustomerName LIKE '_r%';
QUERYING (RETRIEVING) DATA FROM
RELATIONAL
The SQL IN Operator
DATABASES
The IN operator allows you to specify multiple values in a WHERE clause.
The IN operator is a shorthand for multiple OR conditions.

IN Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);

or:

SELECT column_name(s)
FROM table_name
WHERE column_name IN (SELECT STATEMENT);
QUERYING (RETRIEVING) DATA FROM
RELATIONAL
Examples of using IN Operator
DATABASES
The following SQL statement selects all customers that are located in "Germany", "France" and
"UK":

SELECT * FROM Customers


WHERE Country IN ('Germany', 'France', 'UK’);

The following SQL statement selects all customers that are NOT located in "Germany",
"France"
or "UK":

SELECT * FROM Customers


WHERE Country NOT IN ('Germany', 'France', 'UK’);

The following SQL statement selects all customers that are from the same countries as the
suppliers:

SELECT * FROM Customers


WHERE Country IN (SELECT Country FROM Suppliers);
QUERYING (RETRIEVING) DATA FROM
RELATIONAL DATABASES
The SQL 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.

BETWEEN Syntax

SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
QUERYING (RETRIEVING) DATA FROM
RELATIONAL DATABASES
Examples of using BETWEEN Operator

The following SQL statement selects all products with a price BETWEEN 10 and 20:

SELECT * FROM Products


WHERE Price BETWEEN 10 AND 20;

To display the products outside the range of the previous example, use NOT BETWEEN:

SELECT * FROM Products


WHERE Price NOT BETWEEN 10 AND 20;

The following SQL statement selects all orders with an OrderDate BETWEEN '04-July-1996' and
'09-July-1996’:

SELECT * FROM Orders


WHERE OrderDate BETWEEN ’07/04/1996’ AND ’07/09/1996’;
QUERYING (RETRIEVING) DATA FROM
RELATIONAL DATABASES
The SQL AND, OR and NOT Operators
The WHERE clause can be combined with AND, OR, and NOT operators.
The AND and OR operators are used to filter records based on more
than one condition:

• The AND operator displays a record if all the conditions separated


by AND is TRUE.
• The OR operator displays a record if any of the conditions
separated by OR is TRUE.
• The NOT operator displays a record if the condition(s) is NOT
TRUE.
AND Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND
condition3 ...;
OR Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR
condition3 ...;
QUERYING (RETRIEVING) DATA FROM RELATIONAL
DATABASES
Examples of AND, OR and NOT Operators

The following SQL statement selects all fields from "Customers" where country is “UK" AND city
is “London":

SELECT * FROM Customers


WHERE Country=‘UK' AND City=‘London’;

The following SQL statement selects all fields from "Customers" where city is “London" OR
“Liverpool":

SELECT * FROM Customers


WHERE City=‘London' OR City=‘Liverpool’;

The following SQL statement selects all fields from "Customers" where country is NOT “UK":

SELECT * FROM Customers


WHERE NOT Country='Germany';
QUERYING (RETRIEVING) DATA FROM
RELATIONAL DATABASES
• Sorting Records using Order By keyword

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.

ORDER BY Syntax:

SELECT column1, column2, ...


FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
QUERYING (RETRIEVING) DATA FROM
RELATIONAL DATABASES
The following SQL statement selects all customers from the "Customers" table, sorted
by the "Country" column:
SELECT * FROM Customers
ORDER BY Country;

In descending order will be:


SELECT * FROM Customers
ORDER BY Country DESC;

The following SQL statement selects all customers from the "Customers" table, sorted
by the "Country" and the "CustomerName" column:
SELECT * FROM Customers
ORDER BY Country, CustomerName;

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;
QUERYING (RETRIEVING) DATA FROM RELATIONAL
DATABASES
• Handling or testing NULL values

An attribute with a NULL value means no value/missing/unknow.

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.

IS NULL Syntax
SELECT column_names
FROM table_name
WHERE column_name IS NULL;

IS NOT NULL Syntax


SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;
QUERYING (RETRIEVING) DATA FROM
RELATIONAL DATABASES
• Handling or testing NULL values

The following SQL statement uses the IS NULL operator to list all persons that have no
address:

SELECT LastName, FirstName, Address FROM Persons


WHERE Address IS NULL;

The following SQL statement uses the IS NOT NULL operator to list all persons that do
have an address:

SELECT LastName, FirstName, Address FROM Persons


WHERE Address IS NOT NULL;
QUERYING (RETRIEVING) DATA FROM
RELATIONAL DATABASES
• Retrieving information from multiple tables using SQL JOIN

A JOIN clause is used to combine rows from two or more tables, based on a related
column between them.
There four different types of the JOINs in SQL:
1. INNER JOIN: Returns records that have matching values in both tables
2. LEFT (OUTER) JOIN: Return all records from the left table, and the matched records from the right table
3. RIGHT (OUTER) JOIN: Return all records from the right table, and the matched records from the left table
4. FULL (OUTER) JOIN: Return all records when there is a match in either left or right table
QUERYING (RETRIEVING) DATA FROM
RELATIONAL DATABASES
• SQL INNER JOIN Keyword

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;

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;
QUERYING (RETRIEVING) DATA FROM
RELATIONAL DATABASES
• SQL LEFT JOIN Keyword

The LEFT JOIN keyword returns all records from the left table (table1), and the matched
records from the right table (table2). The result is NULL from the right side, if there is
no match.
The LEFT JOIN keyword returns all records from the left table (Customers), even if
there are no matches in the right table (Orders).

LEFT JOIN 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;
QUERYING (RETRIEVING) DATA FROM
RELATIONAL DATABASES
• SQL RIGHT JOIN Keyword

The RIGHT JOIN keyword returns all records from the right table (table2), and the
matched records from the left table (table1). The result is NULL from the left side, when
there is no match.

The RIGHT JOIN keyword returns all records from the right table, even if there are no
matches in the left table.

RIGHT JOIN 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
have placed:

SELECT Orders.OrderID, Employees.LastName, Employees.FirstName


FROM Orders RIGHT JOIN Employees
ON Orders.EmployeeID = Employees.EmployeeID
ORDER BY Orders.OrderID;
QUERYING (RETRIEVING) DATA FROM
RELATIONAL DATABASES
SQL FULL OUTER JOIN Keyword

The FULL OUTER JOIN keyword return all records when there is a match in either left
(table1) or right (table2) table records.

FULL OUTER JOIN Syntax

SELECT column_name(s)
FROM table1 FULL OUTER JOIN table2
ON table1.column_name = table2.column_name;

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;
QUERYING (RETRIEVING) DATA FROM
RELATIONAL DATABASES
• SQL Aggregate Functions

An aggregate function allows you to perform a calculation on a set of values to return a


single scalar value.

We often use aggregate functions with the GROUP BY and HAVING clauses of the
SELECT statement.

The following are the most commonly used SQL aggregate functions:

1. AVG – calculates the average of a set of values.


2. COUNT – counts rows in a specified table or view.
3. MIN – gets the minimum value in a set of values.
4. MAX – gets the maximum value in a set of values.
5. SUM – calculates the sum of values.D
QUERYING (RETRIEVING) DATA FROM
RELATIONAL DATABASES
• SQL aggregate function examples

COUNT function example

To get the number of the products in the products table, you use the COUNT function as follows:

1 SELECT
2 COUNT(*)
3 FROM
4 products;

AVG function example

To calculate the average units in stock of the products, you use the AVG function as follows:

1 SELECT
2 AVG(unitsinstock)
3 FROM
4 products;
QUERYING (RETRIEVING) DATA FROM
RELATIONAL DATABASES
• SQL aggregate function examples

AVG function example

To calculate units in stock by product category, you use the AVG function with the GROUP BY clause as follows:

1 SELECT
2 categoryid, AVG(unitsinstock)
3 FROM
4 products
GROUP BY categoryid;

SUM function example

To calculate the sum of units in stock by product category, you use the SUM function with the GROUP BY clause as
the following query:

1 SELECT categoryid,
2 SUM(unitsinstock) FROM products
3 GROUP BY categoryid;
4
QUERYING (RETRIEVING) DATA FROM
RELATIONAL DATABASES
• SQL aggregate function examples

MIN function example

To get the minimum units in stock of products in the products table, you use the MIN function as follows:

1 SELECT
2 MIN(unitsinstock)
3 FROM
4 products;

MAX function example

To get the maximum units in stock of products in the products table, you use the MAX function as shown in the
following query:

1 SELECT
2 MAX(unitsinstock)
3 FROM products;
4

You might also like