Professional Documents
Culture Documents
Chapter03 - How To Retrieve Data From A Single Table
Chapter03 - How To Retrieve Data From A Single Table
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