You are on page 1of 17

Unit 4: Database Design & Development

SQL - Structured Query Language

1
The SQL LIKE Operator

The LIKE operator is used 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

SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern;
Here are some examples showing different LIKE operators with '%' and '_'
wildcards:

HND in Computing and System Development - Ishani Jayasuriaya

2
Here are some examples showing different LIKE
operators with '%' and '_' wildcards:

SELECT * FROM Customers


WHERE CustomerName LIKE '%a';

SELECT * FROM Customers


WHERE CustomerName NOT LIKE 'a%';

HND in Computing and System Development - Ishani Jayasuriaya

3
SQL 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.
SELECT column_name(s)
FROM table_name
WHERE column_name IN (SELECT STATEMENT);

▪ The following SQL statement selects all customers that are


located in "Germany", "France" and "UK":
SELECT * FROM Customers
WHERE Country IN ('Germany', 'France', 'UK');

HND in Computing and System Development - Ishani Jayasuriaya

4
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.
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;

SELECT * FROM Products


WHERE Price BETWEEN 10 AND 20;

HND in Computing and System Development - Ishani Jayasuriaya

5
SQL Joins

▪ A JOIN clause is used to combine rows from two or more tables, based
on a related column between them.
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID;

HND in Computing and System Development - Ishani Jayasuriaya

6
Different Types of SQL JOINs
▪ Here are the different types of the JOINs in SQL:
▪ (INNER) JOIN: Returns records that have matching values in both tables
▪ LEFT (OUTER) JOIN: Return all records from the left table, and the
matched records from the right table
▪ RIGHT (OUTER) JOIN: Return all records from the right table, and the
matched records from the left table
▪ FULL (OUTER) JOIN: Return all records when there is a match in either left
or right table

HND in Computing and System Development - Ishani Jayasuriaya

7
SQL INNER JOIN Keyword

▪ The INNER JOIN keyword selects records that have matching values in both tables.
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;
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);

HND in Computing and System Development - Ishani Jayasuriaya

8
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.
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 *
FROM Customers LEFT JOIN Orders
ON Customers.CustomerID = Orders.CustomerID
ORDER BY Customers.CustomerName;

HND in Computing and System Development - Ishani Jayasuriaya

9
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.
SELECT column_name(s)
FROM table1
RIGHT JOIN table2 ON table1.column_name = table2.column_nam
e;
▪ The following SQL statement will return all employees, and any orders
they might have have placed:
SELECT *
FROM Orders
RIGHT JOIN Employees ON Orders.EmployeeID =
Employees.EmployeeID
ORDER BY Orders.OrderID;

HND in Computing and System Development - Ishani Jayasuriaya

10
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.
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 *
FROM Customers
FULL OUTER JOIN Orders ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;

HND in Computing and System Development - Ishani Jayasuriaya

11
SQL GROUP BY Statement

▪ 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.

The following SQL statement lists the number of customers in each country:
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country;

HND in Computing and System Development - Ishani Jayasuriaya

12
The SQL HAVING Clause

▪ The HAVING clause was added to SQL because the WHERE keyword could not be used
with aggregate functions.

▪ 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;

HND in Computing and System Development - Ishani Jayasuriaya

13
GRANT and REWOK

▪ Data Control Language(DCL) is used to control privileges in Database. To


perform any operation in the database, such as for creating tables,
sequences or views, a user needs privileges. Privileges are of two types,
▪ System: This includes permissions for creating session, table, etc and all types of
other system privileges.
▪ Object: This includes permissions for any command or query to perform any
operation on the database tables.

14
GRANT

▪ Used to provide any user access privileges or other priviliges for


the database

▪ Allow a User to create table


▪ GRANT CREATE TABLE TO username;

▪ Grant all privilege to a User


▪ GRANT sysdba TO username;

15
REVOKE

▪ Used to take back permissions from any user.

▪ To take back Permissions


▪ REVOKE CREATE TABLE FROM username;

16
Thank You !
To be Continued

17

You might also like