SQL SERVER
Lecture 6 - SELECT STATEMENT - MULTIPLE CRITERIA
• Multiple search criteria can be combined using
the logical operators AND, OR, and NOT. AND &
OR operators allow to join two or more
expressions. The AND returns results when all
conditions are true. The OR returns results when
any of the conditions are true.
Retrieving Rows - Multiple Criteria
SELECT column_list
FROM table_list
WHERE [NOT] expression
{AND|OR} [NOT] expression
SELECT STATEMENT
SELECT [ALL|DISTINCT][TOP <n> [PERCENT]
[WITH TIES]] <column_list>
[INTO <new_table>]
[FROM <table_sources>]
[WHERE <search_condition>]
[GROUP BY group_expression[,...n]
[HAVING <search_condition>]
[ORDER BY {column_name [ASC|DESC]}[,...n]]
COMPLETE SELECT STATEMENT
• SELECT * FROM table_name is the most
basic of all queries.
• Where an asterisk (*) for the column_list will
retrieve all columns from the table.
SELECT STATEMENT
QUERY
SELECT *
FROM employee
OUTPUT
Select all columns & rows
SELECT column_name [, column_name...]
FROM table_name
• To select specific columns, each column must be
separated by a comma (,).
Select Multiple Columns
QUERY
SELECT fname, lname, emp_id
FROM employee
OUTPUT
Select Fname, Lname and Emp_id
• In the results of a query, the column names are
shown as headings as defined in the table
structure.
• Aliasing the column headings make them more
readable.
• If the alias has spaces, it must be enclosed in
single quotation marks or the SQL Server
identifier delimiters [].
Changing Column Name
SELECT column_heading = column_name
FROM table_name
OR
SELECT column_name
[AS]‘column_heading’
FROM table_name
Columns Aliases in SELECT
QUERY SELECT EmployeeID = emp_id, LastName = lname
FROM employee
OR
SELECT emp_id AS EmployeeID, lname AS LastName
FROM employee
OUTPUT
Column Aliases Example
• Where clause applies filter to retrieve desired
rows.
• Where clause followed by a Boolean expression
that selects record.
• Boolean expression can be simple or compound.
Selecting Rows Conditionally
Operator Description
= Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
Comparison Operators
QUERY
select *
from CUSTOMER
where CustomerNumber='1001'
OUTPUT
Customer with Id 1001
QUERY
select *
from CUSTOMER
where AreaCode > 30
OUTPUT
Customer with Id 1001
SELECT column_list
FROM table_list
WHERE column
[NOT] BETWEEN low AND high
• Rows can be retrieved based on a range of
values using the BETWEEN keyword. The
BETWEEN clause is inclusive. NOT operator
retrieve rows out of range
Range – Between Operator
QUERY select *
from CUSTOMER
where CustomerNumber
Between 1000 And 1001
OUTPUT
Customer with Id 1001
SELECT column_list
FROM table_list
WHERE column [NOT] IN (value_list)
• Rows can be retrieved with values that match
those in a list by using the IN keyword. NOT
operator retrieve rows out of range
IN Operator - multiple values
QUERY select *
from CUSTOMER
where CustomerNumber
Between 1000 And 1001
OUTPUT
Customer with Id 1001
SELECT column_list
FROM table_list
WHERE column_name [NOT] LIKE 'string'
• To retrieve rows based on portions of character
strings, use the LIKE keyword
LIKE Operator – Pattern Matching
• SQL wildcards can substitute for one or more
characters when searching for data in a
database. SQL wildcards must be used with the
SQL LIKE operator.
Pattern Matching - Wildcards
Wildcard Description
% or * A substitute for zero or more characters
_ A substitute for exactly one character
[charlist] Any single character in charlist
[^charlist] Any single character not in charlist
Pattern Matching - Wildcards
QUERY
select * from CUSTOMER
where LastName like 'J%'
OUTPUT
Customers with Name starting with letter ‘J’
QUERY
SELECT title_id, title
FROM titles
WHERE title LIKE '%computer%'
OUTPUT
Books with computer anywhere in the title
QUERY
SELECT au_id, au_lname, au_fname
FROM authors
WHERE au_lname LIKE '[BM]%'
OUTPUT
All authors whose names begin with B or M
• A null value is not the same as a blank character
string; nor is it the same as a 0 when dealing
with numeric data. A NULL occurs when a value
is not assigned to a field. This is another way of
saying that NULL is equivalent to the value
"unknown." Records can be discriminated in the
tables containing NULL values by using the IS
NULL and IS NOT NULL keywords.
Null Values
SELECT column_list
FROM table_list
WHERE column_name IS [NOT] NULL
IS NULL and IS NOT NULL
QUERY
SELECT title_id, title
FROM titles
WHERE ytd_sales IS NULL
OUTPUT
Find all books that have no sales