🎉 Use coupon LEARN40 and get 40% OFF on all courses! Limited time — don’t miss out! - Use code:

LEANR40

SQL Cheatsheet

5 min read 3 views 0 comments
SQL Cheatsheet
SQL Cheatsheet

📝 SQL Cheatsheet

A quick reference guide for SQL programming — perfect for beginners and interview revision 📊

This SQL cheatsheet helps you quickly recall commonly used SQL commands for database creation, querying, data manipulation, and performance optimization.

1️⃣ Database Basics

CREATE DATABASE db_name;
USE db_name;

2️⃣ Tables

-- Create table
CREATE TABLE table_name (
  col1 datatype,
  col2 datatype
);

-- Drop table
DROP TABLE table_name;

-- Alter table
ALTER TABLE table_name
ADD column_name datatype;

3️⃣ Insert Data

INSERT INTO table_name (col1, col2)
VALUES (val1, val2);

4️⃣ Select Queries

-- Select all columns
SELECT * FROM table_name;

-- Select specific columns
SELECT col1, col2 FROM table_name;

-- Select with condition
SELECT *
FROM table_name
WHERE condition;

5️⃣ Update Data

UPDATE table_name
SET col1 = value1
WHERE condition;

6️⃣ Delete Data

DELETE FROM table_name
WHERE condition;

7️⃣ Joins

-- Inner Join
SELECT *
FROM table1
INNER JOIN table2
ON table1.col = table2.col;

-- Left Join
SELECT *
FROM table1
LEFT JOIN table2
ON table1.col = table2.col;

-- Right Join
SELECT *
FROM table1
RIGHT JOIN table2
ON table1.col = table2.col;

8️⃣ Aggregations

-- Count rows
SELECT COUNT(*) FROM table_name;

-- Sum values
SELECT SUM(col) FROM table_name;

-- Group by
SELECT col, COUNT(*)
FROM table_name
GROUP BY col;

9️⃣ Sorting & Limiting

-- Order results
SELECT *
FROM table_name
ORDER BY col ASC;  -- or DESC

-- Limit results
SELECT *
FROM table_name
LIMIT n;

🔟 Indexes

-- Create index
CREATE INDEX idx_name
ON table_name (col);

-- Drop index
DROP INDEX idx_name;

1️⃣1️⃣ Subqueries

SELECT *
FROM table_name
WHERE col IN (
  SELECT col FROM other_table
);

1️⃣2️⃣ Views

-- Create view
CREATE VIEW view_name AS
SELECT * FROM table_name;

-- Drop view
DROP VIEW view_name;
💡 Tip: Bookmark this cheatsheet for quick SQL revision before interviews or coding sessions!

Comments (0)

No comments yet

Be the first to share your thoughts!

Leave a Comment

Your email address will not be published.