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

Lecture01-SQL Review-New

Uploaded by

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

Lecture01-SQL Review-New

Uploaded by

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

SQL

Review
Lecture 01
Agenda
• SELECT

• JOIN
SELECT
Fetch Data from a Database
SQL Statements
Data Manipulation Language (DML) SELECT
INSERT
UPDATE
DELETE
Data Definition Language (DDL) CREATE
ALTER
DROP
RENAME
Data Control Language (DCL) GRANT
REVOKE

Transaction Control Language (TCL) COMMIT


ROLLBACK
SAVEPOINT
Select
SELECT [distinct]
column1,
column2,
[expression]
...
FROM
tablename
[WHERE conditions];

• SELECT: identifies the columns or calculations

• FROM: identifies the table that you want to retrieve data from
• WHERE: identifies the condition that has to be met for the record to be
selected.
Select
SELECT customer_id, name, address
FROM customers;

• The above query shows actor_id, first_name, and last_name for all actors in
table customers.

SELECT customer_id, name, address


FROM customers
WHERE first_name = ‘NICK’;

• The above query shows actor_id, first_name, and last_name for actors in
table customer whose first_name is NICK.

SELECT * -- selecting all columns


FROM customers;
Column Alias
SELECT list_price * 1.05 as new_price
FROM products;

SELECT list_price * 1.05 as "New Price"


FROM products;

… …
SORTING
• ORDER BY column_1[,column_2,…]
– Is used to sort the result of a SQL select statement.
– The default order is ascending.

SELECT *
FROM product_categories
ORDER BY category_name;

• Sorting in Descending Order


– Use keyword DESC in the ORDER BY clause
SELECT *
FROM product_categories
ORDER BY category_name DESC;
Sorting By Column Alias
• The result of a SQL statement can be sorted by a column alias.

SELECT list_price * 1.05 as new_price


FROM products
ORDER BY new_price;

SELECT list_price * 1.05 as "New Price"


FROM products
ORDER BY "New Price";
Sorting By Column Numeric Position
SELECT *
FROM product_categories
ORDER BY 2;

• The result of the above query is sorted based on the value of the second
column.
SELECT *
FROM product_categories
ORDER BY 1;

• The result of the above query is sorted based on the value of the first
column.
Sorting By Multiple Columns
SELECT *
FROM warehouses
ORDER BY warehouse_name, location_id DESC;

• The result of the above query is sorted first by column warehouse_name


ascendingly and then by column location_id descendingly.
Simple Arithmetic Expressions
Operator Description Example
+ Add SELECT list_price + 10
FROM products;

- Subtract SELECT list_price - 5


FROM products;
SELECT list_price * 1.05
* Multiply
FROM products;
SELECT list_price / 2
/ Divide
FROM products;


Operator Precedence

*/ Multiplication, division
+- Addition, subtraction
Comparison Operators and Conditions
Operator Description
= Equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
<> (!=) Not equal to
LIKE / NOT LIKE Match a character pattern
BETWEEN / NOT BETWEEN Checks values within a given range (inclusive)
IN / NOT IN Checks multiple distinct values
IS NULL / IS NOT NULL Checks if a column is null
LIKE / NOT LIKE
• The “LIKE” operator is used to select a column with a specific string pattern.

• The following wild cards is used with the LIKE operator.


– %: nothing or anything (zero, one, or more characters)
– _: a single character

SELECT employee_id, first_name, last_name


FROM employees
WHERE last_name LIKE '%s%e';

SELECT employee_id, first_name, last_name


FROM employees
WHERE last_name LIKE '%r_';
Escape Character ‘\’
• To find the character patterns including ‘_’ (underscore) the escape character
‘\’ is used.

SELECT *
FROM class
WHERE course_code LIKE ‘DBS\_%’

• The above condition checks for course_code to starts ‘DBS_’ sub-string


followed by zero, one, or more characters.
BETWEEN / NOT BETWEEN
SELECT product_id, product_name, list_price
FROM products
WHERE list_price BETWEEN 1000 AND 1050

SELECT product_id, product_name, list_price


FROM products
WHERE list_price NOT BETWEEN 50 AND 8000
ORDER BY list_price;
IN/ NOT IN
SELECT location_id, postal_code, city, state
FROM locations
WHERE city IN ('Munich', 'Toronto', 'Tokyo');

SELECT *
FROM product_categories
WHERE category_id not in (2,3);
IS NULL / IS NOT NULL
SELECT location_id, postal_code, city, state
FROM locations
WHERE state IS NULL;

SELECT *
FROM warehouses
WHERE location_id IS NOT NULL;
Logical Operators
Operator Description
NOT returns true if the condition is false
AND returns true if all conditions are true
OR returns true if either condition is true
AND / OR
SELECT product_id, product_name, list_price, category_id
FROM products
WHERE category_id = 4 AND list_price < 280;

SELECT *
FROM warehouses
WHERE warehouse_name = 'Bombay' OR location_id IN (6, 8, 13);
Rules of Precedence
Order of precedence Operator
1 Parentheses
2 */
3 +-
4 =, <, >, <=, >=, <>
5 IS [NOT] NULL, [NOT] LIKE, [NOT] IN
6 BETWEEN
7 NOT
8 AND
9 OR
Concatenation Operator
• The concatenation operator links columns of type character strings.
– Concatenation operator: ||
– Concatenation function: CONCAT(column1, column2)

SELECT first_name, last_name,


first_name || last_name as "Name1",
first_name || ' ' || last_name as "Name2",
CONCAT(first_name,last_name) as "Name 3",
CONCAT(CONCAT(first_name, ' '), last_name) as "Name 4"
FROM contacts;


Literal Character Strings
• Literals such as a character, a number, or a date can be included in SQL select statement.

• Character and date literals must be enclosed by single quotations.

SELECT product_name || ' costs ' || list_price


from products;


SELECT last_name || q'['s job title is ]' || job_title
FROM employees;


Distinct
• Distinct clause is used to remove the duplicate rows from a SQL select query
result.

SELECT DISTINCT state


FROM locations;


Table Structure
• DESC or DESCRIBE
– displays a table structure.

DESC products;
or
DESCRIBE products;

Name Null Type


------------- -------- --------------
PRODUCT_ID NOT NULL NUMBER
PRODUCT_NAME NOT NULL VARCHAR2(255)
DESCRIPTION VARCHAR2(2000)
STANDARD_COST NUMBER(9,2)
LIST_PRICE NUMBER(9,2)
CATEGORY_ID NOT NULL NUMBER
Join
Displaying data from multiple tables
Fetching Data from Multiple Tables
• products


• product_categories
Joins
• What is the category name of each product?
– To find the category name of each product, we need to select from two tables:
– products
– product_categories

• Different types of joins:


– INNER JOIN (JOIN)
– LEFT OUTER JOIN (LEFT JOIN)
– RIGHT OUTER JOIN (RIGHT JOIN)
– FULL OUTER JOIN (FULL JOIN)
INNER JOIN
• INNER JOIN returns all rows from multiple tables if the join condition is
true.

SELECT p.product_id, p.product_name,


c.category_name
FROM products p
INNER JOIN product_categories c
ON p.category_id = c.category_id
ORDER BY p.product_id;
table 1 table 2

Inner Join


INNER JOIN (Example 2)
• What is the category name of products in category 1, 2, and 8.

SELECT p.product_id, p.product_name, c.category_name


FROM products p
INNER JOIN product_categories c
ON p.category_id = c.category_id
WHERE p.product_id in (1,2,8)
ORDER BY p.product_id;
SELF JOIN
• Who is the manager of employee with Id 101?
– The employee Id is located in table employees.
– The manager Id is also located in table employees.
– A manager is also an employee. So, the column manager_id in table employees
refers to the column employee_id in the same table.

• To find the manager name and last name of employee 101, we need to join
the employee table with itself:
SELECT e.employee_id, e.first_name, e.last_name,
m.manager_id, m.first_name, m.last_name
FROM employees e
INNER JOIN employees m
ON e.manager_id = m.employee_id
WHERE e.employee_id = 101;
OUTER JOINs
• Display all customers and their orders. Include the customers without
orders in your result.

• The INNER JOIN will select all rows from both tables as long as there is a
match between the columns we are matching on.
• If a customer has not placed an order or has not placed an order in the time
we might specify, then this customer will not be listed as there is no common
field.

• To solve the problem, an outer join is required.


– LEFT OUTER JOIN
– RIGHT OUTER JOIN
– FULL OUTER JOIN
LEFT / RIGHT OUTER JOIN
• LEFT (or RIGHT) JOIN returns
– The result of the inner join between two tables (all matched rows)
– And any unmatched rows from the left (or right) tables

• Display all customers and their orders. Include the customers without
orders in your result.
SELECT c.customer_id, o.order_id, o.order_date
FROM customers c …
LEFT OUTER JOIN orders o
ON c.customer_id = o.customer_id;


LEFT / RIGHT OUTER JOIN

table l table 2 table 1 table 2

Left Outer Join Right Outer Join


FULL OUTER JOIN
• LEFT (or RIGHT) JOIN returns
– The result of the inner join between two
tables (all matched rows)
– And any non-matching rows from both
left and right tables

SELECT w.warehouse_id,
w.warehouse_name,
l.location_id,
l.city
FROM warehouses w
FULL OUTER JOIN locations l
ON w.location_id = l.location_id;
FULL OUTER JOIN

table 1 table 2

Full Outer Join

You might also like