Reference
SQL Crib SheetThese notes are intended to provide a simplified crib sheet (or reminder) on SQL. It is not a tutorial. A number of examples for common types of tasks are provided - but little or no explanation. SQL - Structured Query Language - is a language understood by most database systems. Except where noted it is believed these SQL statements will work with Microsoft SQL Server, Oracle and MySQL. IndexSelectReturn all records all columns in a table: select * from TableName Return all records but only field1 and field2 in a table: select field1, field2 from TableName Return field1 for all records in a table with a specific value for field2: select field1 from TableName where field2=123 Return all records in a table where field1 is one of three possible values: select * from TableName where field1 in (value1,value2,value3) Return the number of records in a table: select count(*) from TheTable Return the number of records in a table with a specific value for field2: select count(*) from TableName where field2=123 Simple join: select * from table1, table2 or
or
Select all unique values in field1 from a table (not supported in MSAccess): select distinct(field1) from TableName or select distinct field1 from TableName For MSAccess use: SELECT count(*) from (select distinct field1 from TableName) Select all unique values for field1 from a table together with the number of records with that unique value: select field1, count(*) from TableName Select all unique values for combinations of field1 and field2 from a table together with the number of records with that combination: select field1, field2, count(*) from TableName Select the number of unique values: select count(distinct field1) from TableName Select all duplicate records in a table, where two (or more) records are considered duplicates if they share a common value for a single field: select field, count(field) from TableName Select all duplicate records in a table, where two (or more) records are considered duplicates if they share common values for a pair of fields: select field1, field2, count(*) from TableName Select similar records, i.e. all records which have duplicate field1 and field2 in a table but with different field3 (i.e. specifying which fields must be the same and which different): select * from table as A, table as B Note:
Select all records from a table which do not share a common ID with records from a second table: select * from table1 Note:
An alternative using a join (which can be much faster):
The following method (which has been suggested by Michael Miller) is to use EXISTS. It is much faster on SQL Server than the above (but Michael says it is comparable with the left join technique on Oracle):
To perform a two way join:
this has been tested on SQL Server, but not on Oracle or MySql. It does not work with MS-Access. To combine the results of two queries (be aware that the number and types of fields in both queries must agree): select * from table1 To return a value based on the contents of a field. This can be done
using either The following works with MSAccess: select Iif(field1 = 1, 'one', 'not one') This is equivalent to the following on SqlServer: select Case when field1 = 1 then 'One' else 'Two' End For Oracle use the To create a new table to hold the results of the select query: select * into table2 from table1 Be aware that this will fail if table2 exists, and that the new table will be created without any indexes. InsertInsert new record into a table: insert into TableName values (1,2,3) Insert new record into a table explicitly naming fields:
Insert new record into a table using values from another table: insert into TableName (field1,field2,field3) MySQL (but not Oracle or SQL Server) allow a single insert statement to insert multiple rows rather than once at a time:
UpdateUpdate all records in a table: update TableName set field1=2 Update specific records in a table: update TableName set field1=2 where field1=1 To update more than one field at a time: update TableName set field1=2, field2=3 Update a field in a table using a value from another table where both records are referenced by a common key - warning, different databases support different syntax!
or
or
Note:
DeleteDelete all records in a table (dangerous): delete from TableName Delete specific records in a table: delete from TableName where field1=value Delete records from one table which do not have a matching field in another table: delete from TableName where field1 not in KeysBe aware that there are often subtle syntax variations between different database systems. Also other key properties (for example 'clustered') will vary between database systems. Therefore please treat this part of the SQL crib sheet as a guide only. Create a primary key on a table:
To add an index on a field: alter table TableName Add Index (field1) To remove a primary key: alter table drop primary key |
KB >