Skip to content

Commit 35b522c

Browse files
committed
Practice SQL Online
Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 36eec30 commit 35b522c

File tree

4 files changed

+256
-52
lines changed

4 files changed

+256
-52
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,47 @@
1-
I can give general MySQL optimization tips, but for a specific optimization, I need to see your query. Here are some common ways to optimize SQL queries in MySQL:
1+
General MySQL optimization tips.
2+
3+
Here are some common ways to optimize SQL queries in MySQL:
24

35
1. Use Indexing Properly
46
Ensure that frequently used columns in WHERE, JOIN, and ORDER BY clauses have indexes.
57
Use composite indexes if filtering by multiple columns.
6-
2. **Avoid SELECT ***
8+
9+
2. Avoid SELECT
710
Select only the necessary columns to reduce I/O load.
11+
812
Example:
9-
sql
10-
Copy
11-
Edit
13+
1214
SELECT id, name FROM users WHERE status = 'active';
15+
1316
3. Use EXPLAIN for Query Analysis
17+
1418
Run EXPLAIN before your query to check execution plans:
15-
sql
16-
Copy
17-
Edit
19+
1820
EXPLAIN SELECT * FROM orders WHERE customer_id = 123;
1921
Look for "Using filesort" or "Using temporary", which indicate potential optimizations.
20-
4. Optimize JOINS
22+
23+
4. Optimize JOINS:
24+
2125
Ensure joined columns have indexes.
2226
Use INNER JOIN instead of LEFT JOIN when possible.
2327
Avoid joining large tables unnecessarily.
28+
2429
5. Use LIMIT for Large Datasets
2530
Instead of fetching all rows, paginate results:
26-
sql
27-
Copy
28-
Edit
2931
SELECT * FROM products ORDER BY price DESC LIMIT 100;
32+
3033
6. Optimize Subqueries & Use Joins Instead
3134
Convert correlated subqueries into JOINs when possible.
35+
3236
Example (bad):
33-
sql
34-
Copy
35-
Edit
3637
SELECT name FROM customers WHERE id IN (SELECT customer_id FROM orders);
3738
Optimized version:
38-
sql
39-
Copy
40-
Edit
39+
4140
SELECT DISTINCT c.name FROM customers c JOIN orders o ON c.id = o.customer_id;
41+
4242
7. Partitioning Large Tables
4343
For massive datasets, consider table partitioning to improve query speed.
44+
4445
8. Use Proper Data Types
4546
Use INT for numbers instead of VARCHAR.
46-
Use DATETIME instead of VARCHAR for date fields.
47+
Use DATETIME instead of VARCHAR for date fields.
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
1-
most useful and important SQL concepts you should know for efficient querying:
1+
the most useful and important SQL concepts you should know for efficient querying:
22

33
---
44

55
SELECT Clause → Fetch Data
66
Retrieves data from a table.
77

88
SELECT column1, column2 FROM table_name;
9-
Example:
10-
9+
Example:
1110
SELECT name, roll FROM students;
1211

1312
---
1413

1514
FROM Clause → Specify Table:
16-
Indicates the table from which to fetch data.
17-
15+
Indicates the table from which to fetch data.
1816
SELECT * FROM students;
1917

2018
---
2119

2220
WHERE Clause → Filter Data
2321
Retrieves rows based on conditions.
24-
22+
2523
SELECT * FROM students WHERE deptno = 10;
2624

2725
Operators:
@@ -47,57 +45,53 @@ SELECT name, roll FROM students ORDER BY name ASC;
4745
---
4846

4947
LIMIT Clause → Restrict Number of Rows
50-
Fetches a specific number of rows.
48+
49+
Fetches a specific number of rows.
5150

52-
SELECT * FROM students LIMIT 5;
51+
SELECT * FROM students LIMIT 5;
5352

5453
---
5554

56-
JOINs (Combining Tables)*
57-
INNER JOIN (common values in both tables)
58-
59-
SELECT students.name, dept.deptname
60-
FROM students
61-
62-
INNER JOIN dept ON students.deptno = dept.deptno;
55+
JOINs (Combining Tables)
56+
INNER JOIN (common values in both tables)
57+
58+
SELECT students.name, dept.deptname
59+
FROM students
60+
INNER JOIN dept ON students.deptno = dept.deptno;
6361

64-
- LEFT JOIN (all rows from the left table, matching from right)
65-
- RIGHT JOIN (all rows from the right table, matching from left)
62+
- LEFT JOIN (all rows from the left table, matching from right)
63+
- RIGHT JOIN (all rows from the right table, matching from left)
6664

6765
---
6866

6967
GROUP BY & Aggregate Functions → Group & Calculate
7068
- COUNT(), SUM(), AVG(), MAX(), MIN()
7169

7270
SELECT deptno, COUNT(*) FROM students GROUP BY deptno;
73-
7471
---
7572

76-
HAVING Clause* → Filter Groups
77-
Used after GROUP BY (unlike WHERE).
73+
HAVING Clause → Filter Groups
74+
Used after GROUP BY (unlike WHERE).
7875

79-
SELECT deptno, COUNT(*)
80-
FROM students
81-
GROUP BY deptno
82-
HAVING COUNT(*) > 3;
76+
SELECT deptno, COUNT(*)
77+
FROM students
78+
GROUP BY deptno
79+
HAVING COUNT(*) > 3;
8380

8481
---
8582

8683
INSERT, UPDATE, DELETE (Modify Data)
87-
- INSERT → Add new records
88-
84+
- INSERT → Add new records
8985
INSERT INTO students (roll, name, deptno) VALUES (16, 'Ravi', 30);
9086

91-
- UPDATE → Modify existing records
92-
87+
- UPDATE → Modify existing records
9388
UPDATE students SET name = 'Raj' WHERE roll = 1;
9489

95-
- DELETE → Remove records
96-
90+
- DELETE → Remove records
9791
DELETE FROM students WHERE roll = 1;
9892

9993
---
10094

101-
💡 Bonus: Use Indexes to speed up searches:
102-
103-
CREATE INDEX idx_deptno ON students(deptno);
95+
Bonus: Use Indexes to speed up searches:
96+
97+
CREATE INDEX idx_deptno ON students(deptno);
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
🚀 2️⃣ Interactive SQL Playgrounds
2+
You can practice SQL online without installing anything! These platforms help you run queries directly in the browser
3+
and explore SQL in real time.
4+
5+
🔹 SQL Online Editors (Run Queries Directly)
6+
1. SQLite Online – Best for practicing SQLite
7+
2. DB Fiddle – Supports MySQL, PostgreSQL, and SQLite
8+
3. Mode SQL Tutorial – Interactive guided SQL exercises with real examples
9+
4. W3Schools SQL TryIt – Basic SQL practice with simple queries
10+
11+
🔹 Full Databases for Practice
12+
5. LeetCode SQL – SQL problems from real-world scenarios, interview-focused
13+
6. HackerRank SQL – Competitive SQL coding challenges with difficulty levels
14+
7. Kaggle Datasets + SQL – Use SQL on real datasets inside Kaggle Notebooks
15+
16+
Recommendation:
17+
Start with SQLite Online for basic query practice.
18+
Move to HackerRank or LeetCode for advanced, problem-solving style SQL challenges.
19+
Use Kaggle for working with real-world datasets.
20+
21+
------------------------------------------------------------
22+
23+
🚀 3️⃣ SQL Project Ideas (Real-World Use Cases)
24+
Here are some cool SQL-based projects you can try. These projects simulate real-world applications where SQL plays a
25+
critical role.
26+
27+
✅ 1. Student Management System (CRUD App)
28+
Features:
29+
- Add, update, and delete students
30+
- Assign students to departments
31+
- Generate reports (like total students per department)
32+
33+
Tech Stack:
34+
Frontend: React.js / HTML-CSS-JS
35+
Backend: Flask / Node.js
36+
Database: PostgreSQL / MySQL
37+
38+
Key Query:
39+
SELECT deptno, COUNT(*) FROM students GROUP BY deptno;
40+
41+
------------------------------------------------------------
42+
43+
✅ 2. Online Library System
44+
Features:
45+
- Store books, authors, and borrowers
46+
- Track issued and returned books
47+
- Generate overdue book reports
48+
49+
Key Query:
50+
SELECT books.title, borrowers.name
51+
FROM books
52+
JOIN loans ON books.book_id = loans.book_id
53+
JOIN borrowers ON loans.borrower_id = borrowers.id
54+
WHERE loans.return_date IS NULL;
55+
56+
------------------------------------------------------------
57+
58+
✅ 3. E-commerce Sales Dashboard
59+
Features:
60+
- Track customer orders and payments
61+
- Show top-selling products
62+
- Find customers who haven’t ordered in the last 30+ days
63+
64+
Key Query:
65+
SELECT product_name, SUM(quantity) AS total_sold
66+
FROM orders
67+
GROUP BY product_name
68+
ORDER BY total_sold DESC
69+
LIMIT 5;
70+
71+
------------------------------------------------------------
72+
73+
✅ 4. Employee Attendance System
74+
Features:
75+
- Store employee check-in and check-out times
76+
- Calculate total working hours per employee
77+
- Find employees with low attendance records
78+
79+
Key Query:
80+
SELECT employee_id, SUM(hours_worked) AS total_hours
81+
FROM attendance
82+
GROUP BY employee_id
83+
HAVING total_hours < 40;
84+
85+
------------------------------------------------------------
86+
87+
✅ 5. Coding Time Tracker (Like WakaTime)
88+
Features:
89+
- Log programming time per language
90+
- Track daily, weekly, and monthly coding stats
91+
- Show most-used programming languages
92+
93+
Key Query:
94+
SELECT language, SUM(time_spent) AS total_time
95+
FROM coding_logs
96+
GROUP BY language
97+
ORDER BY total_time DESC;
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
real-world SQL problems:
2+
3+
Scenario: University Database
4+
5+
You have two tables:
6+
7+
📌 students Table
8+
9+
| roll | name | city | deptno |
10+
|------|--------|-----------|--------|
11+
| 1 | Smith | Delhi | 10 |
12+
| 2 | Vijay | Kolkata | 10 |
13+
| 3 | Amit | Mumbai | 20 |
14+
| 4 | Priya | Chennai | 30 |
15+
| 5 | Rahul | Pune | 40 |
16+
17+
📌 dept Table
18+
19+
| deptno | deptname |
20+
|--------|----------|
21+
| 10 | CSE |
22+
| 20 | ECE |
23+
| 30 | Mech |
24+
| 40 | Civil |
25+
| 50 | Chemical |
26+
27+
---
28+
29+
1. Find all students from Delhi
30+
SELECT * FROM students WHERE city = 'Delhi';
31+
32+
2. Retrieve names of students studying in department 10
33+
SELECT name FROM students WHERE deptno = 10;
34+
35+
3. Show unique department numbers (without duplicates)
36+
37+
SELECT DISTINCT deptno FROM students;
38+
39+
4. List students in alphabetical order
40+
41+
SELECT name FROM students ORDER BY name ASC;
42+
43+
5. Count the total number of students
44+
45+
SELECT COUNT(*) FROM students;
46+
47+
---
48+
49+
Intermediate Level Problems:
50+
51+
6. Find students whose names start with "A"
52+
53+
SELECT * FROM students WHERE name LIKE 'A%';
54+
55+
7. Get the number of students in each department
56+
57+
SELECT deptno, COUNT(*) FROM students GROUP BY deptno;
58+
59+
8. Find students from either 'Delhi' or 'Mumbai'
60+
61+
SELECT * FROM students WHERE city IN ('Delhi', 'Mumbai');
62+
63+
9. Find students NOT from Kolkata
64+
65+
SELECT * FROM students WHERE city <> 'Kolkata';
66+
67+
10. Find students who have a department assigned (`deptno IS NOT NULL`)
68+
69+
SELECT * FROM students WHERE deptno IS NOT NULL;
70+
71+
72+
Advanced Level Problems
73+
74+
11. Retrieve student names along with their department names (JOIN)
75+
76+
SELECT students.name, dept.deptname
77+
FROM students
78+
INNER JOIN dept ON students.deptno = dept.deptno;
79+
80+
12. Show departments that have at least 2 students
81+
82+
SELECT deptno, COUNT(*)
83+
FROM students
84+
GROUP BY deptno
85+
HAVING COUNT(*) >= 2;
86+
87+
13. Find the **top 3 departments** with the most students
88+
89+
SELECT deptno, COUNT(*)
90+
FROM students
91+
GROUP BY deptno
92+
ORDER BY COUNT(*) DESC
93+
LIMIT 3;
94+
95+
14. Find students without a department (LEFT JOIN)
96+
97+
SELECT students.name, dept.deptname
98+
FROM students
99+
LEFT JOIN dept ON students.deptno = dept.deptno
100+
WHERE dept.deptname IS NULL;
101+
102+
15. Delete students from the 'Civil' department
103+
104+
DELETE FROM students WHERE deptno = (SELECT deptno FROM dept WHERE deptname = 'Civil');
105+
106+
---
107+
108+
Bonus Challenge:
109+
Write a query to find the department with the most students
110+
Retrieve the top 5 students based on roll number, but skip the first 2
111+
112+
---

0 commit comments

Comments
 (0)