SQL
STATEMENTS
QUERY
A query is a request for data or
information from a database table or
combination of tables.
TYPES OF QUERY:
• Data Retrieval Query – use of SELECT queries
• Action Query – operations on the data such as INSERT, UPDATE or
DELETE
SQL SELECT
VARIOUS SQL SELECT SYNTAX
The SELECT DISTINCT
statement is used to return
only distinct (different)
values.
SQL SELECT DISTINCT
VARIOUS SQL SELECT SYNTAX
The SELECT WHERE clause is
used to filter records.
The WHERE clause is used to
extract only those records that
fulfill a specified condition.
OPERATORS in WHERE Clause
Operator Description
= Equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
<> Not equal.
Note: In some versions of SQL this operator may be written as !=
BETWEEN Between a certain range
LIKE Search for a pattern
IN To specify multiple possible values for a column
AND, OR, NOT Logical Operators
SELECT WHERE = EXAMPLE
SELECT WHERE = PRACTICE
1 2
SELECT [section],[course] FROM tblsection
WHERE
[course]='COMPUTER ENGINEERING SELECT [section],[course] FROM tblsection
TECHNOLOGY'; WHERE [yrlvl]=1;
SELECT * from tblstudents
WHERE [studno]=‘20-0002‘;
3 --OR
SELECT * from tblstudents
WHERE [fn]=‘Rangel‘;
SELECT WHERE LIKE EXAMPLE
SELECT WHERE LIKE EXAMPLE
SELECT WHERE IN EXAMPLE
SELECT WHERE BETWEEN INT EXAMPLE
SELECT WHERE BETWEEN DATE EXAMPLE
SELECT WHERE AND EXAMPLE
SELECT WHERE OR EXAMPLE
SELECT WHERE NOT EXAMPLE
SELECT ORDER BY SYNTAX
The ORDER BY keyword is used to sort the result-set in ascending
or descending order.
The ORDER BY keyword sorts the records in ascending order by
default. To sort the records in descending order, use the DESC
keyword.
SYNTAX:
SELECT ORDER BY SYNTAX
SELECT ORDER BY DESC SYNTAX
SELECT ORDER BY DESC SYNTAX
SELECT WHERE ORDER BY DESC
SYNTAX
SELECT COMPUTED COLUMN
A computed column is a virtual column that
is not physically stored in the table, unless
the column is marked PERSISTED.
A computed column expression can use
data from other columns to calculate a
value for the column to which it belongs.
SELECT COMPUTED COLUMN
Literal Character Strings
EXAMPLE Concat(fn,’ ‘,ln) AS fullname,
USE CONCAT() function to connect, link, or combine the data together to produce
more understandable information.
SELECT MIN, MAX
The MIN() function returns the
smallest value of the selected
column.
The MAX() function returns the
largest value of the selected
column.
SELECT COUNT, AVG,SUM
The COUNT() function returns the
number of rows that matches a
specified criterion.
The AVG() function returns the average
value of a numeric column.
The SUM() function returns the total sum
of a numeric column.
SELECT GROUP BY
The GROUP BY statement groups
rows that have the same values
into summary rows, like "find the
number of customers in each
country".
The GROUP BY statement is often
used with aggregate functions
(COUNT(), MAX(), MIN(), SUM(),
AVG()) to group the result-set by
one or more columns.