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

SQL - Select

The document discusses the SELECT statement in SQL Server. It describes the basic components of a SELECT statement - the SELECT, FROM, and WHERE clauses. It provides examples of different types of SELECT statements, including selecting all columns and rows, specific columns, aliasing columns, conditionally selecting rows using comparison operators, the BETWEEN, IN, LIKE operators, and handling NULL values. It also discusses pattern matching with wildcards that can be used with the LIKE operator.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

SQL - Select

The document discusses the SELECT statement in SQL Server. It describes the basic components of a SELECT statement - the SELECT, FROM, and WHERE clauses. It provides examples of different types of SELECT statements, including selecting all columns and rows, specific columns, aliasing columns, conditionally selecting rows using comparison operators, the BETWEEN, IN, LIKE operators, and handling NULL values. It also discusses pattern matching with wildcards that can be used with the LIKE operator.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

SQL SERVER

Lecture 6 - SELECT STATEMENT


SELECT STATEMENT
SELECT statement creates queries to retrieve
information from the database to retrieve rows
and columns from tables in the database. There
are three basic components to the SELECT
statement: SELECT, FROM, and WHERE.
SELECT STATEMENT
SELECT <column_list>
FROM <table_list>
WHERE <search_criteria>
The SELECT clause specifies the columns to retrieve.
The FROM clause specifies the tables from which the columns are
to be retrieved.
The WHERE clause kind of the rows returned by the query.
COMPLETE 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]]
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.
Q
U
E
R
Y
O
U
T
P
U
T
Select all columns & rows
SELECT *
FROM employee
Select Multiple Columns
SELECT column_name [, column_name...]
FROM table_name
To select specific columns, each column must be
separated by a comma (,).
Q
U
E
R
Y
O
U
T
P
U
T
Select Fname, Lname and Emp_id
SELECT fname, lname, emp_id
FROM employee
Changing Column Name
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 [].
Columns Aliases in SELECT
SELECT column_heading = column_name
FROM table_name
OR
SELECT column_name
[AS]column_heading
FROM table_name
Q
U
E
R
Y
O
U
T
P
U
T
Column Aliases Example
SELECT EmployeeID = emp_id, LastName = lname
FROM employee
OR
SELECT emp_id AS EmployeeID, lname AS LastName
FROM employee
Selecting Rows Conditionally
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.
Comparison Operators
Operator Description
= Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
Q
U
E
R
Y
O
U
T
P
U
T
Customer with Id 1001
select *
from CUSTOMER
where CustomerNumber='1001'
Q
U
E
R
Y
O
U
T
P
U
T
Customer with Id 1001
select *
from CUSTOMER
where AreaCode > 30
Range Between Operator
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
Q
U
E
R
Y
O
U
T
P
U
T
Customer with Id 1001
select *
from CUSTOMER
where CustomerNumber
Between 1000 And 1001
IN Operator - multiple values
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
Q
U
E
R
Y
O
U
T
P
U
T
Customer with Id 1001
select *
from CUSTOMER
where CustomerNumber
Between 1000 And 1001
LIKE Operator Pattern Matching
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
Pattern Matching - Wildcards
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
Q
U
E
R
Y
O
U
T
P
U
T
Customers with Name starting with letter J
select * from CUSTOMER
where LastName like 'J%'
Q
U
E
R
Y
O
U
T
P
U
T
Books with computer anywhere in the title
SELECT title_id, title
FROM titles
WHERE title LIKE '%computer%'
Q
U
E
R
Y
O
U
T
P
U
T
All authors whose names begin with B or M
SELECT au_id, au_lname, au_fname
FROM authors
WHERE au_lname LIKE '[BM]%'
Null Values
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.
IS NULL and IS NOT NULL
SELECT column_list
FROM table_list
WHERE column_name IS [NOT] NULL
Q
U
E
R
Y
O
U
T
P
U
T
Find all books that have no sales
SELECT title_id, title
FROM titles
WHERE ytd_sales IS NULL

You might also like