Fourth Lecture
Fourth Lecture
To get the rows from the table that satisfy one or more conditions, you use
the WHERE clause as follows:
The WHERE clause only returns the rows that cause the search condition to
evaluate to TRUE.
SELECT
product_id,
product_name,
category_id,
model_year,
list_price
FROM
production.products
WHERE
category_id = 1
ORDER BY
list_price DESC;
Finding rows that meet two conditions
SELECT
product_id,
product_name,
category_id,
model_year,
list_price
FROM
production.products
WHERE
category_id = 1 AND model_year = 2018
ORDER BY
list_price DESC;
list_price DESC;
To test whether a value is NULL or not, you always use the IS NULL operator.
SELECT
customer_id,
first_name,
last_name,
phone
FROM
sales.customers
WHERE
phone IS NULL
ORDER BY
first_name,
last_name;
As you may guess, to check if a value is not NULL, you can use the IS NOT
NULL operator.
The following query returns customers who have phone information:
SELECT
customer_id,
first_name,
last_name,
phone
FROM
sales.customers
WHERE
phone IS NOT NULL
ORDER BY
first_name,
last_name;
SELECT
*
FROM
production.products
WHERE
category_id = 1
AND list_price > 400
ORDER BY
list_price DESC;
SELECT
*
FROM
production.products
WHERE
category_id = 1
AND list_price > 400
AND brand_id = 1
ORDER BY
list_price DESC;
SELECT
*
FROM
production.products
WHERE
brand_id = 1
OR brand_id = 2
AND list_price > 1000
ORDER BY
brand_id DESC;
To get the product whose brand id is one or two and list price is larger than
1,000, you use parentheses as follows:
SELECT
*
FROM
production.products
WHERE
(brand_id = 1 OR brand_id = 2)
AND list_price > 1000
ORDER BY
brand_id;
SELECT
product_name,
list_price
FROM
production.products
WHERE
list_price < 200
OR list_price > 6000
ORDER BY
list_price;
SELECT
product_name,
brand_id
FROM
production.products
WHERE
brand_id = 1
OR brand_id = 2
OR brand_id = 4
ORDER BY
brand_id DESC;
SELECT
product_name,
brand_id,
list_price
FROM
production.products
WHERE
brand_id = 1
OR brand_id = 2
AND list_price > 500
ORDER BY
brand_id DESC,
list_price;
SELECT
product_name,
brand_id,
list_price
FROM
production.products
WHERE
(brand_id = 1 OR brand_id = 2)
AND list_price > 500
ORDER BY
brand_id;
SELECT
product_name,
list_price
FROM
production.products
WHERE
list_price IN (89.99, 109.99, 159.99)
ORDER BY
list_price;
SELECT
product_name,
list_price
FROM
production.products
WHERE
list_price = 89.99 OR list_price = 109.99 OR list_price =
159.99
ORDER BY
list_price;
SELECT
product_name,
list_price
FROM
production.products
WHERE
list_price NOT IN (89.99, 109.99, 159.99)
ORDER BY
list_price;