Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 35

Chapter 3

How to retrieve data from a single table


Objectives
• An introduction to the SELECT statement
• How to code the SELECT clause
• How to code the WHERE clause
• How to code the ORDER BY clause
• How to code the LIMIT clause
An Introduction to the
SELECT statement
The basic syntax of the
SELECT statement
SELECT select_list
[FROM table_source]
[WHERE search_condition]
[ORDER BY Order_by_list ]
[LIMIT row_limit]
SELECT statement examples
SELECT statement examples - 2
How to code the SELECT clause
How to code column
specifications
How to name the columns in a
result set using aliases
Use AS
keyword
How to code
arithmetic expressions
• You can code arithmetic expressions to
calculating the result in the query.
Examples use arithmetic
expression in query
How to use the CONCAT
function to join strings

12/31
How to use functions
• Types of functions
– System functions (Built-in functions)
– User defined functions
• Syntax: function name (parameters)
• Example:
SELECT InvoiceDate, GETDATE() AS ‘Today’,
DATEDIFF(day, InvoiceDate, GETDATE()) as Age

13/31
Examples use LEFT and
DATE_FORMAT functions
Examples use ROUND functions
How to use the DISTINCT keyword
to eliminate duplicate rows
How to code the WHERE clause

17/31
How to use comparison operators
• Syntax: Expression_1 operator expression_2
• The comparison operators:
=, > , < , >=, <=, <>

• Examples
SELECT VendorCity, VendorState
FROM Vendors
WHERE VendorState = ‘IA’;
Examples use WHERE clause
How to use the AND, OR and
NOT logical operators
More examples
How to use the IN operator
How to use
the BETWEEN operator
How to use LIKE and REGEXP
operators
More examples
How to use IS NULL clause
How to code the ORDER BY clause

27/31
How to sort a result set
by a column name
• ORDER BY syntax
• ASC / DESC
• Multi columns
• Examples
Examples use ORDER BY Clause
Examples use ORDER BY
Clause -2
How to sort by an alias,
expression or column number
How to code the LIMIT clause
The LIMIT clause specifies the maximum number
of rows that are returned in the result set.
How to return a range of rows
More example
Summary

• SELECT clause
• WHERE clause
– Logical operators
– Comparison operators
– IN, BETWEEN, LIKE operators
– IS NULL
• ORDER BY clause
• Limit Clause

35/31

You might also like