“SQL Must-Know Survival Kit”
critical concepts & habits and
nomenclature (the correct terms) so you can talk SQL like a pro while writing it safely and
efficiently.
SQL Nomenclature — Speak the Language
Term Meaning Example
Database Container for all your data objects USE SalesDB;
Logical grouping of objects inside a
Schema dbo.Customers
DB
Stores rows (records) and columns
Table CREATE TABLE Orders (...)
(fields)
Row / Record One data entry in a table INSERT INTO Orders VALUES (...)
Column /
Attribute of the data price, order_date
Field
Primary Key
Unique identifier for each row customer_id in Customers
(PK)
Foreign Key
Links rows between tables customer_id in Orders → Customers
(FK)
DDL Data Definition Language (structure) CREATE, ALTER, DROP
DML Data Manipulation Language (data) SELECT, INSERT, UPDATE, DELETE
Data Query Language (read-only
DQL SELECT
queries)
DCL Data Control Language (permissions) GRANT, REVOKE
TCL Transaction Control Language BEGIN TRAN, COMMIT, ROLLBACK
Any SQL statement that retrieves or
Query SELECT * FROM Products;
manipulates data
Outermost query returning final
Main Query SELECT name FROM Customers ...
results
Subquery Query inside another query (SELECT AVG(price) FROM Products)
Alias Temporary name for a table/column FROM Customers AS c
Join Combine rows from multiple tables INNER JOIN Orders ON ...
Common Table Expression — named
CTE WITH RecentOrders AS (...)
subquery
View Saved query that acts like a table CREATE VIEW TopCustomers AS ...
CREATE INDEX idx_order_date ON
Index Data structure to speed up lookups
Orders(order_date)
Must-Know SQL Server Tips (Beyond Basics)
1. Always Control Your Context
• Put USE DatabaseName; GO at the top of every script.
• Check the SSMS dropdown before running anything.
2. Separate Structure from Data
• Keep CREATE/ALTER scripts in one file, INSERT/UPDATE/DELETE in another.
• Use GO between unrelated commands.
3. Preview Before You Change
• Write the SELECT version of your UPDATE/DELETE first.
• Add TOP 10 to preview without touching all rows.
4. Transactions Are Your Safety Net
BEGIN TRAN;
-- your change here
ROLLBACK; -- undo if wrong
-- COMMIT; -- only when sure
5. Avoid Duplicate Inserts
• Use TRUNCATE TABLE in dev before re-loading.
• Or wrap inserts in IF NOT EXISTS or MERGE.
6. NULL Awareness
• = doesn’t work with NULL — use IS NULL or IS NOT NULL.
• Aggregates ignore NULLs unless you handle them.
7. Date Filtering Best Practice
• Use >= start_date AND < next_day for ranges to avoid time-of-day issues.
8. Don’t Overuse SELECT *
• Explicitly list columns — faster, safer, and future-proof.
9. Index Wisely
• Index columns used in WHERE, JOIN, and ORDER BY — but not every column.
• Check execution plans (Ctrl+L) before adding indexes.
10. Know Your Joins
• INNER JOIN → only matching rows
• LEFT JOIN → all from left, matches from right
• RIGHT JOIN → all from right, matches from left
• FULL OUTER JOIN → all rows from both sides
• CROSS JOIN → every combination (use with caution)
SQL Learning Approach — The “Concept Ladder”
1. SELECT basics → columns, aliases, WHERE, ORDER BY, LIMIT/TOP
2. Filtering logic → AND, OR, IN, BETWEEN, LIKE, NULL handling
3. Aggregations → COUNT, SUM, AVG, MIN, MAX, GROUP BY, HAVING
4. Joins → INNER, LEFT, RIGHT, FULL, CROSS
5. Subqueries → in WHERE, FROM, SELECT
6. CTEs → readable multi-step queries
7. Window functions → RANK, ROW_NUMBER, LAG, LEAD
8. CRUD → INSERT, UPDATE, DELETE, TRUNCATE
9. DDL → CREATE/ALTER/DROP tables, constraints, indexes
10. Performance → execution plans, indexing strategy, avoiding scans
Quick “Before Execute” Mental Checklist
• Am I in the right database?
• Did I preview with SELECT?
• Did I count intended rows?
• Is my WHERE clause specific?
• Am I in a transaction if risky?
• Did I separate batches with GO?
• Did I guard against duplicates?
SQL Server Command & Concept Map
what each command does,
which category it belongs to, and the “must-know” habits
SQL Server Command & Concept Map
1. SQL Command Categories
Category Purpose Common Commands Example
DDL (Data Definition Define / change CREATE, ALTER, DROP, CREATE TABLE
Language) structure TRUNCATE Customers (...)
DML (Data
INSERT, UPDATE, DELETE, INSERT INTO Orders
Manipulation Change data
MERGE VALUES (...)
Language)
DQL (Data Query SELECT name FROM
Retrieve data SELECT
Language) Products
DCL (Data Control GRANT SELECT ON
Permissions GRANT, REVOKE
Language) Customers TO Analyst
TCL (Transaction Manage BEGIN TRAN, COMMIT, BEGIN TRAN; UPDATE ...;
Control Language) transactions ROLLBACK, SAVEPOINT COMMIT;
2. SQL Building Blocks
Concept What it is Example
Database Container for all objects USE SalesDB;
Schema Logical grouping inside DB dbo.Customers
Table Rows & columns CREATE TABLE Orders (...)
Row One record (1, 'Asha', 'Akola')
Column Attribute price, order_date
Primary Key Unique row ID customer_id
Foreign Key Link to another table customer_id in Orders → Customers
Index Speeds up lookups CREATE INDEX idx_date ON Orders(order_date)
View Saved query CREATE VIEW TopCustomers AS ...
CTE Named temp result WITH RecentOrders AS (...)
Concept What it is Example
Subquery Query inside query (SELECT AVG(price) FROM Products)
3. Query Flow
1. FROM → pick tables
2. JOIN → combine data
3. WHERE → filter rows
4. GROUP BY → group rows
5. HAVING → filter groups
6. SELECT → choose columns
7. ORDER BY → sort results
(Remember: SQL Server processes queries in this logical order, not the order you write them.)
4. Must-Know Habits
• Always USE DatabaseName; GO at the top.
• Preview changes with SELECT before UPDATE/DELETE.
• Wrap risky changes in BEGIN TRAN → ROLLBACK/COMMIT.
• Avoid SELECT * — list columns explicitly.
• Use >= start AND < end for date ranges.
• Guard against duplicates with IF NOT EXISTS or MERGE.
• Check @@ROWCOUNT after changes.
• Use Ctrl+L for estimated execution plan before running heavy queries.
5. Join Types at a Glance
Join Returns
INNER Only matching rows
LEFT All from left + matches from right
RIGHT All from right + matches from left
FULL All rows from both sides
CROSS Every combination (Cartesian)
6. Quick Syntax Reminders
-- SELECT
SELECT col1, col2
FROM dbo.Table
WHERE condition
ORDER BY col1 DESC;
-- INSERT
INSERT INTO dbo.Table (col1, col2)
VALUES (val1, val2);
-- UPDATE
UPDATE dbo.Table
SET col1 = new_val
WHERE condition;
-- DELETE
DELETE FROM dbo.Table
WHERE condition;
-- CREATE TABLE
CREATE TABLE dbo.Table (
id INT PRIMARY KEY,
name NVARCHAR(50) NOT NULL
);
7. Safety Checklist (SAFE-GO)
• S: Set database (USE …; GO)
• A: Activate safety (BEGIN TRAN; SET XACT_ABORT ON)
• F: Fence batches (GO)
• E: Examine with SELECT, COUNT, Plan
• G: Guard duplicates
• O: OK to apply → verify → COMMIT/ROLLBACK
Vishal’s SQL Learning Dashboard (Tracker Format)
Notes /
Concept / Definition (Plain Practice
Example Query Status Errors to
Skill English) Task
Avoid
List all Not
product Started /
Pull specific Avoid SELECT
SELECT SELECT name, price names and
columns from a * in
Basics FROM Products; prices sorted Practicing /
table production
by price
DESC Mastered
Get orders
SELECT * FROM Use IS NULL
Filtering Limit rows based from Pune
Orders WHERE city = for null
(WHERE) on conditions after
'Akola'; checks
2025-01-01
Show top 5
Default is
Sorting Arrange results in most
ORDER BY price DESC ASC if not
(ORDER BY) a specific order expensive
specified
products
SELECT category,
Find total Remember
AVG(price) FROM
Aggregations Summarize data sales per HAVING
Products GROUP BY
customer filters groups
category;
Combine data INNER JOIN Orders
List customer Always check
Joins from multiple ON Customers.id =
names with join keys to
tables Orders.customer_id
Notes /
Concept / Definition (Plain Practice
Example Query Status Errors to
Skill English) Task
Avoid
their order avoid
dates duplicates
WHERE price > Products
Query inside Parentheses
Subqueries (SELECT AVG(price) above
another query required
FROM Products) average price
Get
Named WITH Recent AS (...)
customers Improves
CTEs temporary result SELECT * FROM
with orders in readability
sets Recent;
last 30 days
ROW_NUMBER() Rank
Row-by-row Needs
Window OVER (PARTITION BY products by
calculations over OVER()
Functions category ORDER BY price within
a set clause
price DESC) category
Match
INSERT INTO
Add a new column order
INSERT Add new rows Customers VALUES
product and data
(1,'Asha','Akola');
types
UPDATE Orders SET Cancel Always
Change existing
UPDATE status='Cancelled' orders before preview with
rows
WHERE id=1; 2025-01-01 SELECT first
Use WHERE
DELETE FROM Orders Delete test or risk
DELETE Remove rows
WHERE id=1; data deleting all
rows
Group changes Update
BEGIN TRAN; ... Rollback if
Transactions with multiple
COMMIT; unsure
commit/rollback tables safely
Index a
CREATE INDEX Too many
Speed up frequently
Indexes idx_date ON indexes slow
lookups filtered
Orders(order_date); writes
column
Create a view Acts like a
CREATE VIEW
Views Saved queries for high-value table but is
TopCustomers AS ...
customers read-only
Compare
Look for
Execution See how SQL plan
Ctrl+L in SSMS scans vs
Plans runs your query before/after
seeks
adding index
How to Use This Dashboard
1. Pick one concept at a time — mark it Practicing.
2. Read the definition in plain English.
3. Run the example query in SSMS.
4. Do the practice task with your own data or the sample ecommerce schema we built
earlier.
5. Log mistakes in the “Notes” column — this is your personal error pattern library.
6. Mark as Mastered only when you can do it without looking at notes.
Pro Tip — Layered Learning
• Day 1–5: SELECT, WHERE, ORDER BY, Aggregations
• Day 6–10: Joins, Subqueries, CTEs
• Day 11–15: Window Functions, CRUD (INSERT/UPDATE/DELETE)
• Day 16–20: Transactions, Indexes, Views, Execution Plans
• Day 21+: Mix concepts in mini-projects (e.g., “Customer Purchase Dashboard”)
“SQL Workout Plan”..
Vishal’s SQL Learning Dashboard — Pre-Filled Practice Set
Notes /
Concept / Practice Tasks
Definition Example Query Status Errors to
Skill (Run in SSMS)
Avoid
1. List all
product names
and prices
Retrieve specific sorted by price Avoid SELECT
SELECT SELECT name, price
columns from a DESC * in
Basics FROM Products;
table 2. Show only production
product names
from category
'Beverage'
1. Get orders
from Pune after
Use IS NULL
Filtering Limit rows based SELECT * FROM Orders '2025-01-01'
for null
(WHERE) on conditions WHERE city = 'Akola'; 2. Find products
checks
priced between
100 and 500
1. Show top 5
most expensive
SELECT name, price products Default is
Sorting Arrange results in
FROM Products ORDER 2. List ASC if not
(ORDER BY) a specific order
BY price DESC; customers specified
alphabetically
by name
1. Find total
SELECT category,
sales per Remember
AVG(price) FROM
Aggregations Summarize data customer HAVING
Products GROUP BY
2. Count orders filters groups
category;
per city
1. List customer
SELECT c.name,
names with
o.order_date FROM Always check
Combine data their order
Customers c INNER join keys to
Joins from multiple dates
JOIN Orders o ON avoid
tables 2. Show product
c.customer_id = duplicates
names in each
o.customer_id;
order
Notes /
Concept / Practice Tasks
Definition Example Query Status Errors to
Skill (Run in SSMS)
Avoid
1. Products
above average
SELECT name FROM
price
Query inside Products WHERE price Parentheses
Subqueries 2. Customers
another query > (SELECT AVG(price) required
who placed
FROM Products);
more than 2
orders
1. Get
customers with
WITH RecentOrders AS
orders in last 30
Named (SELECT * FROM Orders
days Improves
CTEs temporary result WHERE order_date >
2. Find top 3 readability
sets '2025-08-01') SELECT *
products by
FROM RecentOrders;
sales in each
category
1. Rank
SELECT name, price, products by
Row-by-row RANK() OVER (ORDER price Needs
Window
calculations over BY price DESC) AS 2. Show OVER()
Functions
a set price_rank FROM cumulative clause
Products; sales per
customer
1. Add a new Match
INSERT INTO
product column order
INSERT Add new rows Customers VALUES
2. Add a new and data
(4,'Vikram','Delhi');
customer types
1. Cancel
orders before
UPDATE Orders SET Always
Change existing '2025-01-01'
UPDATE status='Cancelled' preview with
rows 2. Increase
WHERE order_id=101; SELECT first
price of 'Yoga
Mat' by 10%
1. Delete test
Use WHERE
data
DELETE FROM Orders or risk
DELETE Remove rows 2. Remove
WHERE order_id=103; deleting all
customers with
rows
no orders
1. Update
Group changes
BEGIN TRAN; UPDATE multiple tables Rollback if
Transactions with
...; COMMIT; safely unsure
commit/rollback
2. Rollback a
Notes /
Concept / Practice Tasks
Definition Example Query Status Errors to
Skill (Run in SSMS)
Avoid
mistaken
update
1. Index a
frequently
filtered column Too many
Speed up CREATE INDEX idx_date
Indexes 2. Compare indexes slow
lookups ON Orders(order_date);
query speed writes
before/after
index
1. Create a view
for high-value
CREATE VIEW Acts like a
customers
Views Saved queries TopCustomers AS table but is
2. Create a view
SELECT ...; read-only
for monthly
sales totals
1. Compare
plan
before/after Look for
Execution See how SQL
Ctrl+L in SSMS adding index scans vs
Plans runs your query
2. Identify table seeks
scans in a slow
query
How to Use This
1. Pick one concept → mark it Practicing.
2. Run the example query in SSMS.
3. Do the practice tasks using your sample ecommerce schema (or real data if safe).
4. Log mistakes in “Notes” — this becomes your personal error pattern library.
5. Mark Mastered only when you can do it without looking at notes.
Pro Tip for You, Vishal
Because you already use tracker-driven learning, you can:
• Add columns for “Time Taken” and “Confidence Level” (1–5) to measure speed and
comfort.
• Tag each row with Beginner / Intermediate / Advanced so you can cycle through difficulty
levels.
• Once a week, review all Mastered concepts and do 1–2 “mixed drills” combining them
— this cements retention.