0% found this document useful (0 votes)
8 views9 pages

Fourth Lecture

Uploaded by

irfan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views9 pages

Fourth Lecture

Uploaded by

irfan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

 WHERE NULL AND OR IN BETWEEN LIKE

Introduction to SQL Server WHERE clause


When you use the SELECT statement to query data against a table, you get all
the rows of that table, which is unnecessary because the application may only
process a set of rows at the time.

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.

Finding rows by using a simple equality

The following statement retrieves all products with the category id 1:

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;

Finding rows by using a comparison operator


SELECT
product_id,
product_name,
category_id,
model_year,
list_price
FROM
production.products
WHERE
list_price > 300 AND model_year = 2018
ORDER BY
list_price DESC;

Finding rows that meet any of two conditions


SELECT
product_id,
product_name,
category_id,
model_year,
list_price
FROM
production.products
WHERE
list_price > 3000 OR model_year = 2018
ORDER BY
list_price DESC;

Finding rows with the value between two values


SELECT
product_id,
product_name,
category_id,
model_year,
list_price
FROM
production.products
WHERE
list_price BETWEEN 1899.00 AND 1999.99
ORDER BY

list_price DESC;

Finding rows that have a value in a list of values


SELECT
product_id,
product_name,
category_id,
model_year,
list_price
FROM
production.products
WHERE
list_price IN (299.99, 369.99, 489.99)
ORDER BY
list_price DESC;

Finding rows whose values contain a string


SELECT
product_id,
product_name,
category_id,
model_year,
list_price
FROM
production.products
WHERE
product_name LIKE '%Cruiser%'
ORDER BY
list_price;

SQL Server NULL


You will also learn how to use IS NULL and IS NOT NULL operators to test
whether a value is NULL or not.
In the database world, NULL is used to indicate the absence of any data value.
For example, at the time of recording the customer information, the email may
be unknown, so it is recorded as NULL in the database.

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;

Introduction to SQL Server AND operator


The AND is a logical operator that allows you to combine two Boolean
expressions. It returns TRUE only when both expressions evaluate to TRUE.

A) Using AND operator example


The following example finds the products where the category identification
number is one and the list price is greater than 400:

SELECT
*
FROM
production.products
WHERE
category_id = 1
AND list_price > 400
ORDER BY
list_price DESC;

Using multiple AND operators example


The following statement finds the products that meet all the following
conditions: category id is 1, the list price is greater than 400, and the brand id
is 1:

SELECT
*
FROM
production.products
WHERE
category_id = 1
AND list_price > 400
AND brand_id = 1
ORDER BY
list_price DESC;

Using the AND operator with other logical operators


See the following query example:

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;

Introduction to SQL Server OR operator


The SQL Server OR is a logical operator that allows you to combine two
Boolean expressions. It returns TRUE when either of the conditions evaluates
to TRUE.

Using OR operator example


The following example finds the products whose list price is less than 200 or
greater than 6,000:

SELECT
product_name,
list_price
FROM
production.products
WHERE
list_price < 200
OR list_price > 6000
ORDER BY
list_price;

Using multiple OR operators example


The following statement finds the products whose brand id is 1, 2, or 4:

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;

You can replace multiple OR operators by the IN operator as shown in the


following query:
SELECT
product_name,
brand_id
FROM
production.products
WHERE
brand_id IN (1, 2, 3)
ORDER BY
brand_id DESC;

Using OR operator with AND operator example


Consider the following example:

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;

SQL Server IN operator overview


The IN operator is a logical operator that allows you to test whether a
specified value matches any value in a list.

Using SQL Server IN with a list of values example


The following statement finds the products whose list price is one of the
following values: 89.99, 109.99, and 159.99:

SELECT
product_name,
list_price
FROM
production.products
WHERE
list_price IN (89.99, 109.99, 159.99)
ORDER BY
list_price;

The query above is equivalent to the following query that uses


the OR operator instead:

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;

You might also like