1๏ธโฃ SELECT โ Pull data from a table
_Syntax:_
SELECT column1, column2 FROM table_name;
_Example:_
SELECT name, city FROM customers;
To get _everything_ use:
SELECT * FROM customers;
2๏ธโฃ WHERE โ Filter specific rows
_Syntax:_
SELECT columns FROM table_name WHERE condition;
_Example:_
SELECT name FROM customers WHERE city = 'Delhi';
_Operators you can use:_
โข =, !=, >, <, >=, <=
โข LIKE (pattern match)
โข BETWEEN, IN, IS NULL
3๏ธโฃ ORDER BY โ Sort results
_Syntax:_
SELECT columns FROM table_name ORDER BY column ASC|DESC;
_Example:_
SELECT name, age FROM employees ORDER BY age DESC;
4๏ธโฃ LIMIT โ Restrict number of results
_Syntax:_
SELECT columns FROM table_name LIMIT number;
_Example:_
SELECT * FROM products LIMIT 5;
๐ฅ _Quick Practice Task:_
Write a query to:
โข Get top 10 highest-paid employees in 'Marketing'
โข Show name, salary, and department
โข Sort salary high to low:
SELECT name, salary, department
FROM employees
WHERE department = 'Marketing'
ORDER BY salary DESC
LIMIT 10;
โ SQL Interview QA ๐ผ๐ง
Q1. What does the SELECT statement do in SQL?
_Answer:_
It retrieves data from one or more columns in a table.
SELECT name, city FROM customers;
Q2. How would you fetch all the columns from a table?
_Answer:_
Use SELECT * to get every column.
SELECT * FROM orders;
Q3. Whatโs the difference between WHERE and HAVING?
_Answer:_
โข WHERE filters rows _before_ grouping
โข HAVING filters _after_ GROUP BY
You use WHERE with raw data, HAVING with aggregated data.
Q4. Write a query to find all products with price > 500.
_Answer:_
SELECT * FROM products WHERE price > 500;
Q5. How do you sort data by two columns?
_Answer:_
Use ORDER BY col1, col2.
SELECT name, department FROM employees ORDER BY department ASC, name ASC;
Q6. What does LIMIT 1 do in a query?
_Answer:_
It returns only the _first row_ of the result.
SELECT * FROM customers ORDER BY created_at DESC LIMIT 1;
Q7. Write a query to get names of top 5 students by marks.
_Answer:_
SELECT name, marks
FROM students
ORDER BY marks DESC
LIMIT 5;
Q8. Can you use ORDER BY without WHERE?
_Answer:_
Yes. ORDER BY works independently. It sorts all data unless filtered with WHERE.
๐ก _Pro Tip:_
In interviews, they may ask you to _write queries live_ or explain the _output_ of a query. Stay calm, read the structure carefully, and _think in steps_.
DOUBLE TAP โค๏ธ FOR MORE