Lecture01-SQL Review-New
Lecture01-SQL Review-New
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
• 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.
• The above query shows actor_id, first_name, and last_name for actors in
table customer whose first_name is NICK.
… …
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;
• 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;
…
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.
SELECT *
FROM class
WHERE course_code LIKE ‘DBS\_%’
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)
…
Literal Character Strings
• Literals such as a character, a number, or a date can be included in SQL select statement.
…
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.
…
Table Structure
• DESC or DESCRIBE
displays a table structure.
DESC products;
or
DESCRIBE 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
Inner Join
…
INNER JOIN (Example 2)
• What is the category name of products in category 1, 2, and 8.
• 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.
• 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
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