Monday 9 January 2012

Some SQL Query

1.)Return all records all columns in a table:

select * from TableName

2.)Return all records but only field1 and field2 in a table:

select field1, field2 from TableName

3.)Return field1 for all records in a table with a specific value for field2:

   select field1 from TableName where field2=123

4.) Return all records in a table where field1 is one of three possible values:

   select * from TableName where field1 in (value1,value2,value3)
5.)Return the number of records in a table:

select count(*) from TheTable
6.)Return the number of records in a table with a specific value for field2:

select count(*) from TableName where field2=123


7.)Simple join:

select * from table1, table2
where table1.field1=table2.fieldA
or

select table1.field1, table2.fieldA from table1, table2
where table1.field2=table2.fieldB

or

select table1.field1, table2.fieldA
from table1 inner join table2 on table1.field2 = table2.fieldB

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
group by field1

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
group by field1, field2

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
group by field
having count(*) > 1

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
group by field1, field2
having count(*) > 1

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
where A.field1=B.field1
and A.field2=B.field2
and A.field3<>B.field3;

Note:

It is important to specify at least one field which is different between the two records otherwise this query will list a record as being the same as itself.
This query will not find duplicate records, i.e. records with every field the same.
Select all records from a table which do not share a common ID with records from a second table:

select * from table1
where field1 not in (select field2 from table2)

Note:

Sub-queries are quite slow.
Sub-queries are not supported in versions of MySQL prior to MySQL 5, so the above will not work on older versions of MySQL. My thanks to Kevin Bowman for pointing out that MySQL 5 supports sub-queries.
An alternative using a join (which can be much faster):

select table1.* from table1
left join table2 on (table1.field1 = table2.field2)
where table2.field2 is null;

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):

select * from table1
where not exists (select field2 from table2 where table2.field2 = table1.field1)

8.)To perform a two way join:

select * from
table1 left join table2 on (table1.field1 = table2.field1),
table1 left join table3 on (table1.field2 = table3.field3)

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
union select * from table2

To return a value based on the contents of a field. This can be done using either Iif, Decode or Case, depending on the database.

The following works with MSAccess:

select Iif(field1 = 1, 'one', 'not one')
from TableName

This is equivalent to the following on SqlServer:

select Case when field1 = 1 then 'One' else 'Two' End
from TableName
For Oracle use the DECODE function.

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.

No comments:

Post a Comment