Here is my list of basic SQL that I would expect anyone who writes SQL for me to be able to do.
1.First a straight up select with no joins (and no select *)
SELECT field1, field2
FROM table1
2.You should know how to combine two or more tables and get records that are in all the tables
SELECT a.field1, b.field2
FROM table1 a
JOIN table2 b ON a.id = b.id
3.You should know how to combine two or more tables and get records that are in all the tables but return only one record from the table with the many side of the one-to-many relationship
SELECT a.field1, b.field2
FROM table1 a
LEFT JOIN (
SELECT id, MIN(Field2) as Field2 FROM table2
GROUP BY id
)b
ON a.id = b.id
4.You should be able to get the records in one table but not in an associated table
SELECT a.id, a.field1
FROM Table1 a
WHERE NOT EXISTS
(SELECT *
FROM TABLE2 b
WHERE a.ID = b.ID)
5.You should be able to Aggregate data for a report
SELECT a.field1, b.field2, Sum(Field3)
FROM table1 a
JOIN table2 b ON a.id = b.id
GROUP BY a.field1, b.field2
6.You should be able to insert one record to a table
INSERT TABLE1 (Field1, field2)
VALUES ('test', 1)
7.You should be able to update one record in a table
UPDATE table1
SET Field1 = 'mytest'
WHERE ID = 10
8.You should be able to delete one record in a table
DELETE table1
WHERE ID = 10
9.You should be able to insert a group of records to a table without a cursor
INSERT table1 (field1)
SELECT field1 FROM table2
Or
INSERT table1(field1)
SELECT 1
UNION ALL
SELECT 2
10.You should be able to update a group of records in a table without a cursor
UPDATE t1
SET field1 = t2.field2
FROM table1 t1
JOIN table2 t2 on t1.id = t2.id
WHERE t2.field3 = 'CA'
11.You should be able to delete a group of records in a table without a cursor
DELETE table1
WHERE State = 'VA'
12.You should be able to perform multiple actions in one transaction and handle error trapping
BEGIN TRAN
BEGIN TRY
INSERT table1 (field1)
VALUES ('a')
INSERT table2 (field2)
VALUES ('b')
COMMIT TRAN
END TRY
BEGIN CATCH
ROLLBACK TRAN
PRINT 'oops'
END CATCH
13.You should be able to create union of records and know when to use UNION vice UNION ALL
(Union when you will have records that are duplicated in the two parts of the union and you want to filter them out, UNION ALL when the recordsets would be mutually exclusive or you want to see the dups. UNION ALL is faster so should be used if at all possible.)
SELECT Field1 from table1
UNION
SELECT Field1 from Table2
SELECT Field1 from table1
UNION ALL
SELECT Field2 from Table2
14.You should be able to vary the data for one field based on some criteria (using CASE)
SELECT id, CASE WHEN field1 = 'a' THEN 'Good'
WHEN field1 = 'b' THEN 'BAD'
END as Status
FROM table1
15.You should be able to write an IF Statement.
(this assumes it is in a stored proc and the variables are sent in as input variables)
IF @test = 1
BEGIN
SELECT field1 FROM table1
END
ELSE
BEGIN
SELECT field3 FROM table2
END