You are on page 1of 7

SQL, the

Structured
Query Language
SQL: SELECT Statement
A basic SELECT statement includes 3 clauses

SELECT <attribute name>


FROM <tables> WHERE <condition>
SELECT FROM WHERE

Specifies the Specifies the Specifies the


attributes that are tables that serve selection condition.
part of the as the input to the
resulting relation statement
SQL: SELECT Statement (cont.)
Using a “*” in a select statement indicates
that every attribute of the input table is to be
selected.
Example: SELECT * FROM … WHERE …;

To get unique rows, type the keyword


DISTINCT after SELECT.
Example: SELECT DISTINCT(columnname)
FROM … WHERE …;
Example: 1) SELECT *
Person FROM person
Name Age Weight WHERE age > 30;
Harry 34 80 Name Age Weight
Sally 28 64 Harry 34 80
George 29 70 Helena 54 54
Helena 54 54 Peter 34 80
Peter 34 80

2) SELECT weight 3) SELECT distinct weight


FROM person FROM person
WHERE age > 30; WHERE age > 30;
Weight Weight
80 80
54 54
80
LIKE Operator: (Matching a character pattern)

When one does not know the exact value for the search conditions and wishes to
use a replaceable parameter a LIKE operator can be used. The replaceable
parameters used are shown bellow.
Symbol Represents
% Any sequence or more characters
_ Any single character

Example:
1) To list employees whose names begin with ‘U’
select * from emp where ename like ‘U%’ ;

2) To list employees who name start with ‘Kum’ and ends with ‘r’ and one letter between
that;
select * from emp where ename like ‘Kum_r’;
SQL: Like operation
Pattern matching selection
% (arbitrary string)
SELECT *
FROM emp
WHERE ID like ‘%01’;
 finds ID that ends with 01, e.g. 1001, 2001, etc
_ (a single character)
SELECT *
FROM emp
WHERE ID like ‘_01_’;
 finds ID that has the second and third character
as 01, e.g. 1010, 1011, 1012, 1013, etc
SQL: The ORDER BY Clause
Ordered result selection
desc (descending order)
SELECT *
FROM emp
order by state desc
 puts state in descending order, e.g. TN, MA, CA
asc (ascending order)
SELECT *
FROM emp
order by id asc
 puts ID in ascending order, e.g. 1001, 1002, 1003

You might also like