SQL Cheatsheet
Code snippets for common tasks with SQL Server and SQL Express
Updated: 04 October 2023
Mostly applies to SQL Server and SQL Express
Log Into Instance
1#sqlcmd -S <YOUR DATABASE NAME> -E2sqlcmd -S localhost\SQLEXPRESS -E
Instance Level Operations
List Instance Databases
1SELECT [name] FROM [master].[dbo].[sysdatabases]2GO
Create Database
1CREATE DATABASE [TestDatabase]2GO
Drop Database
1DROP DATABASE TestDatabase2GO
Database Level Operations
List Database Tables
1SELECT [TABLE_NAME]2FROM [TestDatabase].[INFORMATION_SCHEMA].[TABLES]3WHERE [TABLE_TYPE] = 'BASE TABLE'4GO
List Columns in Table
1SELECT * FROM [TestDatabase].[INFORMATION_SCHEMA].[COLUMNS]2 WHERE [TABLE_NAME] = 'Persons'3GO
Create Table
1CREATE TABLE [TestDatabase].[dbo].[Persons] (2 PersonId int,3 LastName varchar(255),4 FirstName varchar(255),5 Address varchar(255),6 City varchar(255)7)8GO
Update Column Data Type
1ALTER Table [TestDatabase].[dbo].[Persons]2 ALTER COLUMN [PersonId] int NOT NULL3GO
Add Column Constraint
1ALTER Table [TestDatabase].[dbo].[Persons]2 ADD CONSTRAINT PK_Person PRIMARY KEY ([PersonId])3GO
Create Table with Links
1CREATE TABLE [TestDatabase].[dbo].[Items]2 (3 ItemId int NOT NULL,4 PersonId int NOT NULL,5 Name nvarchar(50),6 CONSTRAINT PK_Items PRIMARY KEY (ItemId),7 CONSTRAINT FK_Items_Person FOREIGN KEY ([PersonId])8 REFERENCES [Persons] ([PersonId])9 )10GO
Drop Table
1DROP TABLE [Persons]2GO
Insert Item into Table
1INSERT INTO [TestDatabase].[dbo].[Persons]2 ([PersonId], [LastName], [FirstName], [Address], [City])3VALUES (1, 'Name', 'Surname', 'Home', 'Place')4GO
Retrieve Table Values
We can retrieve all values from a table with:
1SELECT TOP (10) [PersonId]2 ,[LastName]3 ,[FirstName]4 ,[Address]5 ,[City]6 FROM [TestDatabase].[dbo].[Persons]7GO
We can get a specific set of values with a condition
1SELECT *2FROM [TestDatabase].[dbo].[Persons]3WHERE [FirstName] = 'John'
Or search for a pattern in a field with LIKE
:
1SELECT *2FROM [TestDatabase].[dbo].[Persons]3WHERE [FirstName] LIKE '%John%'
Update Table Item
1UPDATE [TestDatabase].[dbo].[Persons]2SET [FirstName] = 'John', [LastName] = 'Smith'3WHERE [PersonId] = 14GO
Values in List
We can use the IN
operator to select some data based on values being in a given list
1SELECT * FROM users2WHERE id in (1,2,3)
Testing Statements
When running SQL queries it may sometimes be necessary to check if your query will work as expected before you actually run it you can wrap your query in:
1BEGIN TRANSACTION2 ... DO STUFF3ROLLBACK
ROLLBACK
will roll back to the DB status before the query was carried out
And once you have verified that the query did what you expected, you can change the ROLLBACK
to COMMIT
1BEGIN TRANSACTION2 ... DO STUFF3COMMIT
We can test a deletion of a Person
and view the result with:
1BEGIN TRANSACTION2
3SELECT * FROM [TestDatabase].[dbo].[Persons]4
5DELETE FROM [TestDatabase].[dbo].[Persons]6 WHERE [LastName] = 'Person2'7
8SELECT * FROM [TestDatabase].[dbo].[Persons]9
10ROLLBACK
And we can then COMMIT
this when we are sure it works
1BEGIN TRANSACTION2
3SELECT * FROM [TestDatabase].[dbo].[Persons]4
5DELETE FROM [TestDatabase].[dbo].[Persons]6 WHERE [LastName] = 'Person2'7
8SELECT * FROM [TestDatabase].[dbo].[Persons]9
10COMMIT
Table Joining
Inner Join
To use an Inner Join based on two tables we can use the INNER JOIN
keywords and then get the fields from the tables we want to use for our output table:
1SELECT2a.FirstName as FirstName,3a.Email as Email,4a.ID as ID,5
6b.Vehicle as Vehicle,7b.Registered as IsRegistered8
9FROM Table_1 as a10INNER JOIN Table_2 as b11ON a.ID = b.UserId
Inner Queries
You can use subqueries inside of SQL queries for the purpose of comparing data against without actually returning/selecting the data from the inner query
1SELECT *2FROM users3WHERE id IN4 (5 SELECT user_id6 FROM orders7 WHERE order_id IN (1,3)8 )9AND LOWER(username) LIKE LOWER('%bob%')