Вы находитесь на странице: 1из 1

SQL Quick Reference

Format Example Notes

Querying with SELECT

select * from table1 select * from company All columns and rows in table1

select col1, col2 select name, code Specific columns col1 and col2
from table1 from company

select * select * Specific rows


from table1 from company
where condition where cap > 50

select * f rom t
able select * from company Orders results in descending order.

order by c ol1 d esc order by cap desc Use asc for ascending.

WHERE syntax

where col1 = val1 select * from company Operators: =, <, >, <=, >=, in,
where code = 'CBA' between, like.

where col1 like pattern select * from company x% starts with x


where name like 'a%' %x ends with x
%x% contains x

where condition1 and select * from company Conditions can be joined with and
condition2 where cap > 50 and or or.
sector = 'Materials'

where not condition1 select * from company where Return everything not matching the
not sector = 'Materials' condition.

Aggregating and grouping

select sum(col1) from t1 select sum(cap) from company Common aggregate functions are
count, sum, avg, min, max.

select sum(col1) from t1 select sum(cap) from company Groups rows by col2, then applies
group by col2 group by sector the function to each group of col1.

Joining tables with WHERE and JOIN

select * from t1, t2 select * from company, return Inner join includes rows that have a
where t1.id = t2.id where id = companyID match in both t1 and t2.

select * from t1 left select * from company left Left join includes all records from t1,
join t2 on t1.id = t2.id join return on id = companyID even when there is no match in t2

Updating tables with INSERT, UPDATE, DELETE

insert into t1 (col1, insert into company (name, Insert a row into t1
col2) values (val1, val2) code) values ('acme', 'XYZ')

update t1 set col1 = update company Update rows matching condition


val1, col2 = val2 set name = 'acmi', cap = 32 with new values
where condition where code = 'XYZ'

delete from t1 where delete from company Delete rows from t1 matching
condition where code = 'XYZ' condition

Вам также может понравиться