I use SQL statements on an almost daily basis, yet I have trouble remembering the syntax of even the most basic commands. Thus I decided to post a cheat sheet here for quick and easy reference. I hope you may find it valuable.
Selecting Records | SELECT * FROM Table WHERE Param1 < 10 ORDER BY Param2 |
Selecting Records Into a New Table | SELECT * INTO newdb.dbo.newtable FROM olddb.dbo.oldtable |
Updating Records | UPDATE Table SET Param1 = ‘cat’, Param3 = ‘fish’ WHERE Param2 = ‘dog’ |
Adding Records | INSERT INTO Table (Param1, Param2) VALUES (‘xx’, ‘yy’) |
Deleting Records | DELETE FROM Table WHERE Param1 = ‘value’ |
Group By | SELECT column_name, aggregate_function(column_name) FROM table_name WHERE column_name = value GROUP BY column_name |
List all tables & views in database | SELECT TABLE_TYPE, TABLE_SCHEMA, TABLE_NAME FROM INFORMATION_SCHEMA.TABLES ORDER BY TABLE_TYPE, TABLE_NAME |