SQL Queries 🔍📊⚡
Ask the right question, get the right answer. SQL queries help you filter, sort and find exactly the data you need!
Day 41: SQL Queries — Ask the Right Question, Get the Right Answer!
What's a Query?
Imagine a cricket scoreboard with 1000 players. You don't read every row manually. You ask a precise question — "show me the top scorers, highest first." The operator searches the entire database and hands you exactly what you asked for in milliseconds. A SQL query is that precise question. Ask it right, get exactly what you need!
ORDER BY — Sort Your Results
import sqlite3
conn = sqlite3.connect("rohithbuilds.db")
cursor = conn.cursor()
cursor.execute("INSERT OR IGNORE INTO students VALUES ('Rohith', 87)")
cursor.execute("INSERT OR IGNORE INTO students VALUES ('Arjun', 45)")
cursor.execute("INSERT OR IGNORE INTO students VALUES ('Sneha', 92)")
conn.commit()
cursor.execute("SELECT name, score FROM students ORDER BY score DESC")
for row in cursor.fetchall():
print(f"{row[0]} -> {row[1]}")
Output: Sneha 92, Rohith 87, Arjun 45. ORDER BY score DESC sorts highest first. Without DESC it sorts lowest first by default. This is exactly how leaderboards in every game and app work!
Combining WHERE and ORDER BY
cursor.execute("SELECT name, score FROM students WHERE score >= 50 ORDER BY score DESC")
for row in cursor.fetchall():
print(f"{row[0]} -> {row[1]}")
Output: Sneha 92, Rohith 87. WHERE filters first — only scores above 50. ORDER BY then sorts those results highest first. Arjun with 45 is filtered out completely. Database does all the work — your Python just prints the result!
LIMIT — Get Only What You Need
cursor.execute("SELECT name, score FROM students ORDER BY score DESC LIMIT 2")
for row in cursor.fetchall():
print(f"{row[0]} -> {row[1]}")
Output: Sneha 92, Rohith 87. LIMIT 2 returns only the top 2 results. This is how Instagram shows your top 3 suggested friends. How Swiggy shows the top 10 restaurants nearby. Slice exactly what you need!
Real World Connection
PUBG leaderboard — SELECT name, kills FROM players ORDER BY kills DESC LIMIT 100. Swiggy showing nearby restaurants — SELECT name FROM restaurants WHERE city = 'Hyderabad' ORDER BY rating DESC. YouTube trending — SELECT title FROM videos WHERE views > 1000000 ORDER BY views DESC LIMIT 20. Every feed, every leaderboard, every recommendation is a SQL query!
Common Mistakes
Mistake 1 — Missing comma between column names.
SELECT name score FROM students # WRONG — syntax error!
SELECT name, score FROM students # CORRECT — comma required!
Mistake 2 — Forgetting DESC for highest first.
ORDER BY score # WRONG — returns lowest first by default!
ORDER BY score DESC # CORRECT — highest values appear first!
Mini Challenge
Mini Challenge
Add 5 students with different scores. Write 3 separate queries: one to show all students sorted highest score first, one to show only students who passed (score >= 50), and one to show just the top 2 scorers using LIMIT. You just built the query system that powers every school ranking app and game leaderboard!
Quick Quiz
Q: What does ORDER BY score DESC do? A: Sorts results from highest score to lowest — highest first!
Q: How do you get only the top 3 results? A: Add LIMIT 3 at the end of your query!
Q: What's the order of clauses in a full SQL query? A: SELECT → FROM → WHERE → ORDER BY → LIMIT!
Key Takeaways
Key Takeaways
- ORDER BY sorts results. ASC = lowest first (default). DESC = highest first.
- WHERE filters rows. ORDER BY sorts them. LIMIT cuts to top N results.
- Always use commas between column names in SELECT.
- Queries go in order — SELECT, FROM, WHERE, ORDER BY, LIMIT.
- Every leaderboard, feed and recommendation in every app is just a SQL query!
Continue Learning with Rohi
You've used your 3 free Rohi questions. Create a free account to continue learning.