Thursday, 24 March 2011

SQL Select Clauses Crib Sheet

Adding to yesterday's simple crib-sheet, today's blog is my SQL clauses crib-sheet...

SELECT

select *
select column1, column2, ...
select distinct(column1)
select count(*)

Defines columns to display as part of a query.

FROM

from table1, table2, table3,...

Defines tables from which to retrieve data.

WHERE

where column1 = 'value1'
and column2 = 'value2'

...

where column1 = 'value1'
or column2 = 'value2'

Defines conditions, criteria, placed upon a query for data to be returned.

GROUP BY

group by group_column1, group_column2,...

A form of sorting operation; used to divide output into logical groups.

HAVING

having group_column1 = 'value'
and group_column2 = 'value' ...

Used to sort a query's results.

ORDER BY

order by column1, column2,...
order by 1,2,3...

No comments: