<DataInsights />
  • 🏠 Home
  • 📊 SQL
  • 🐍 Python
  • 📈 Power BI
  • 📗 Excel
  • 💼 Career
  • 🎯 Interview Q&A
  • 📁 Case Study
  • 📥 Downloads
  • 🚀 My Portfolio
<DataInsights />

Practical Data Analytics tutorials covering SQL, Python, Power BI, Excel and career guidance for aspiring analysts — 100% free.

Topics

  • SQL Tutorials
  • Python Guide
  • Power BI
  • Excel Tips
  • Career Guide

Quick Links

  • 🛠️ All Tools
  • 🗓️ Archive
  • 📬 Contact
  • 🔍 Search
  • Portfolio
  • Kaggle
  • GitHub

Legal & Info

  • About
  • Contact
  • Privacy Policy
  • Disclaimer
  • Terms & Conditions
  • DMCA
  • Sitemap
Copyright © 2026 Data Insights by Jatin Kumar. All Rights Reserved.Built with ❤️ for Data Analysts
Home/Interview Q&A/Top 30 Basic SQL Interview Questions Ans Answers...

Top 30 Basic SQL Interview Questions Ans Answers

A
August 4, 2026 Jatin Kumar 25 min read Interview Q&A
Data Insights Interview Prep — SQL Basic

Top 30 Basic SQL Interview Questions & Answers

SQL interviews ka pehla round — Basic concepts. SELECT, WHERE, ORDER BY, JOINs basics, Keys, Constraints, Data Types, NULL handling — sab kuch detailed answers aur queries ke saath. Freshers aur 0-1 year experience ke liye perfect preparation guide. Data Insights par.

📑 Topics Covered in This Blog:

  • SQL Basics — What, Why, Types (Q1-Q5)
  • SELECT, WHERE, Filtering — Core Queries (Q6-Q12)
  • Sorting, Limiting, Aliases (Q13-Q16)
  • NULL Handling (Q17-Q19)
  • Constraints & Keys (Q20-Q24)
  • Data Types & Table Operations (Q25-Q28)
  • Basic Functions & Miscellaneous (Q29-Q30)

📋 Level: Basic — Freshers & 0-1 Year Experience. Yeh questions almost har SQL interview ke pehle round mein puchhe jaate hain. Har question ka detailed answer (English), explanation (Hinglish), aur SQL query (code block) diya gaya hai.

Q1: What is SQL?

Answer: SQL (Structured Query Language) is a standard programming language used to manage, manipulate, and retrieve data from Relational Database Management Systems (RDBMS). It allows users to create databases, tables, insert data, update records, delete data, and query information using structured commands.

🎯 Explanation: SQL ek language hai jo databases se baat karne ke liye use hoti hai. Jaise tum Google se kuch search karte ho English mein — waise hi tum database se data maangte ho SQL mein. "Mujhe saare employees ka naam do jinka salary 50000 se zyada hai" — yeh tum SQL mein likh sakte ho aur database tumhe answer dega. Har company ka data databases mein stored hota hai — SQL usse access karne ka standard tarika hai.

Q2: What are the different types of SQL commands?

Answer: SQL commands are categorized into 5 types based on their functionality:

Type Full Form Commands Purpose
DDL Data Definition Language CREATE, ALTER, DROP, TRUNCATE Database/Table structure define karna
DML Data Manipulation Language INSERT, UPDATE, DELETE Data ko add, modify, remove karna
DQL Data Query Language SELECT Data retrieve/fetch karna
DCL Data Control Language GRANT, REVOKE Permissions/access control
TCL Transaction Control Language COMMIT, ROLLBACK, SAVEPOINT Transaction management

🎯 Explanation: Socho SQL ek toolbox hai — usme alag alag tools hain alag kaam ke liye. DDL se table banana/badalna hai (jaise ghar ki structure banate ho). DML se data dalna/badalna hai (jaise ghar mein furniture rakhte ho). DQL se data nikalna hai (jaise ghar mein kya hai dekhte ho). DCL se permission dena hai (jaise ghar ki chaabi dete ho). TCL se transactions manage karna hai (jaise payment confirm ya cancel karte ho). Interview mein yeh question almost 100% puchha jaata hai!

Q3: What is the difference between SQL and MySQL?

Answer: SQL is a language (standard) used to communicate with databases. MySQL is a software (RDBMS) that uses SQL as its query language. SQL is the language, MySQL is one of the tools that understands that language. Other tools include PostgreSQL, SQL Server, Oracle, SQLite.

🎯 Explanation: SQL ek language hai — jaise English ek language hai. MySQL ek software hai — jaise Google Translate ek tool hai jo English samajhta hai. SQL likhte ho commands — MySQL woh commands execute karta hai aur results deta hai. Bahut log confuse hote hain — "SQL aur MySQL same nahi hai?" Nahi bhai! SQL = bhasha, MySQL = tool. PostgreSQL, Oracle bhi SQL samajhte hain — lekin woh alag software hain.

Q4: What is a Database and RDBMS?

Answer: A Database is an organized collection of structured data stored electronically. An RDBMS (Relational Database Management System) is a software that stores data in tables (rows and columns) with relationships between them. Examples: MySQL, PostgreSQL, SQL Server, Oracle.

🎯 Explanation: Database = organized data ka collection. Jaise tumhare phone mein Contacts app hai — saare contacts organized hain naam, number, email ke saath. Yeh ek database hai! RDBMS = woh software jo is data ko tables mein rakhta hai aur relationships banata hai. "Relational" matlab tables ek doosre se connected hain — jaise Employees table aur Departments table Department_ID se connected hain.

Q5: What is the difference between DROP, DELETE, and TRUNCATE?

Answer:

Feature DELETE TRUNCATE DROP
Type DML DDL DDL
What it removes Specific rows (with WHERE) All rows (data only) Entire table (structure + data)
Table structure Remains Remains Removed completely
WHERE clause ✅ Yes ❌ No ❌ No
Rollback ✅ Possible ❌ Not possible ❌ Not possible
Speed Slow (row by row) Fast (bulk operation) Fast

🎯 Explanation: DELETE = rubber se specific lines mita do (notebook baaki hai). TRUNCATE = poore notebook ke pages faad do (notebook cover baaki hai, phir se likh sakte ho). DROP = poori notebook phenko — kuch nahi bachega. DELETE mein WHERE laga sakte ho — "sirf yeh row hatao." TRUNCATE mein sab data ek baar mein jaata hai. DROP mein table hi khatam. Yeh question 90% interviews mein puchha jaata hai!

-- DELETE: Remove specific rows DELETE FROM employees WHERE department = 'HR';
-- TRUNCATE: Remove ALL rows, keep table structure
TRUNCATE TABLE employees;
-- DROP: Remove entire table (structure + data)
DROP TABLE employees;

Q6: How do you retrieve all data from a table?

Answer: Use the SELECT * statement. The asterisk (*) is a wildcard that represents all columns in the table.

🎯 Explanation: SELECT * FROM table_name — yeh sabse basic SQL query hai. * matlab "sab columns do." FROM ke baad table ka naam likho. Result mein table ki saari rows aur saare columns aa jayenge. Production mein * avoid karo — specific columns likho performance ke liye. Lekin testing/exploration mein * use hota hai.

-- Retrieve all data from employees table
SELECT *
FROM employees;
-- Better practice: Specify columns
SELECT emp_id, name, salary
FROM employees;

Q7: What is the WHERE clause?

Answer: The WHERE clause is used to filter records based on a specified condition. It returns only those rows that satisfy the given condition. It can be used with SELECT, UPDATE, DELETE, and other statements.

🎯 Explanation: WHERE ek filter hai — "sirf woh rows dikhao jo is condition ko satisfy karti hain." Jaise tum Amazon par filter lagate ho "Price under ₹500" — waise hi SQL mein WHERE lagao "Salary > 50000." Bina WHERE ke SELECT saari rows dega. WHERE ke saath sirf matching rows aayengi. Operators use kar sakte ho: =, !=, >, <, >=, <=, BETWEEN, IN, LIKE, IS NULL.

-- Filter employees with salary greater than 50000
SELECT *
FROM employees
WHERE salary > 50000;
-- Filter by department name
SELECT *
FROM employees
WHERE department = 'Sales';
-- Multiple conditions with AND/OR
SELECT *
FROM employees
WHERE department = 'Sales' AND salary > 50000;

Q8: What is the difference between WHERE and HAVING?

Answer: WHERE filters individual rows BEFORE grouping (works on raw data). HAVING filters groups AFTER the GROUP BY operation (works on aggregated data). WHERE cannot use aggregate functions, HAVING can.

🎯 Explanation: WHERE = pehle filter karo, phir group karo. HAVING = pehle group karo, phir filter karo. Example: "Sales department ke employees dikhao" — yeh WHERE se hoga (individual rows filter). "Woh departments dikhao jinki average salary 60000 se zyada hai" — yeh HAVING se hoga (groups par filter). WHERE mein SUM(), AVG() nahi likh sakte — HAVING mein likh sakte ho.

-- WHERE: Filter rows BEFORE grouping
SELECT department, AVG(salary)
FROM employees
WHERE city = 'Mumbai'
GROUP BY department;
-- HAVING: Filter groups AFTER grouping
SELECT department, AVG(salary) AS avg_sal
FROM employees
GROUP BY department
HAVING AVG(salary) > 60000;

Q9: What is DISTINCT and when do you use it?

Answer: DISTINCT removes duplicate values from the result set and returns only unique values. It is used with SELECT to get a list of unique entries in a column or combination of columns.

🎯 Explanation: Agar employees table mein department column mein "Sales" 10 baar likha hai — normal SELECT 10 baar "Sales" dikhayega. DISTINCT lagao — ek baar "Sales" dikhayega. Unique values chahiye toh DISTINCT use karo. "Kitne unique departments hain?" — SELECT DISTINCT department FROM employees;

-- Get unique department names
SELECT DISTINCT department
FROM employees;
-- Count unique departments
SELECT COUNT(DISTINCT department)
FROM employees;
-- Unique combination of department + city
SELECT DISTINCT department, city
FROM employees;

Q10: What are the different operators used in WHERE clause?

Answer: WHERE clause supports multiple types of operators for filtering data:

Operator Purpose Example
=, !=, <> Equal, Not equal WHERE dept = 'Sales'
>, <, >=, <= Comparison WHERE salary > 50000
BETWEEN Range (inclusive) WHERE salary BETWEEN 30000 AND 70000
IN Match any value in list WHERE city IN ('Delhi', 'Mumbai')
LIKE Pattern matching WHERE name LIKE 'A%'
IS NULL / IS NOT NULL Check for NULL values WHERE email IS NULL
AND, OR, NOT Logical operators WHERE dept = 'IT' AND salary > 50000

🎯 Explanation: WHERE clause mein yeh sab operators use karke data filter karte hain. LIKE mein % = kuch bhi characters, _ = ek character. 'A%' matlab A se start hone wale names. '%kumar%' matlab beech mein "kumar" ho. IN multiple values check karta hai — OR ka shortcut hai. BETWEEN inclusive hai — dono boundaries include hoti hain.

Q11: What is the LIKE operator and explain wildcards?

Answer: LIKE is used for pattern matching in string columns. It uses two wildcards: % (matches zero or more characters) and _ (matches exactly one character).

🎯 Explanation: LIKE pattern matching ke liye hai — exact match nahi chahiye, partial match chahiye tab use karo. % = "kuch bhi aa sakta hai" (zero ya zyada characters). _ = "exactly ek character." 'A%' = A se start (Amit, Anil, Asha). '%a' = a par end (Priya, Neha). '_a%' = second character 'a' ho (Ravi, Sahil). Interview mein LIKE ke patterns puchhe jaate hain!

-- Names starting with 'A'
SELECT *
FROM employees
WHERE name LIKE 'A%';
-- Names ending with 'kumar'
SELECT *
FROM employees
WHERE name LIKE '%kumar';
-- Names containing 'raj' anywhere
SELECT *
FROM employees
WHERE name LIKE '%raj%';
-- Names with exactly 5 characters
SELECT *
FROM employees
WHERE name LIKE '_____';
-- Second character is 'a'
SELECT *
FROM employees
WHERE name LIKE '_a%';

Q12: What is the difference between IN and BETWEEN?

Answer: IN checks if a value matches any value in a specified list (discrete values). BETWEEN checks if a value falls within a range (continuous range, inclusive of both boundaries).

🎯 Explanation: IN = "kya yeh value is list mein hai?" — jaise city IN ('Delhi', 'Mumbai', 'Chennai'). Specific values match karte ho. BETWEEN = "kya yeh value is range mein hai?" — jaise salary BETWEEN 30000 AND 70000. Continuous range hai, dono boundaries include hain. IN discrete values ke liye, BETWEEN continuous range ke liye.

-- IN: Match specific values from a list
SELECT *
FROM employees
WHERE city IN ('Delhi', 'Mumbai', 'Chennai');
-- BETWEEN: Match a continuous range (inclusive)
SELECT *
FROM employees
WHERE salary BETWEEN 30000 AND 70000;
-- BETWEEN with dates
SELECT *
FROM orders
WHERE order_date BETWEEN '2024-01-01' AND '2024-12-31';

Q13: What is ORDER BY clause?

Answer: ORDER BY sorts the result set based on one or more columns. ASC (ascending — default) sorts from smallest to largest. DESC (descending) sorts from largest to smallest. It is always the last clause in a query (before LIMIT).

🎯 Explanation: ORDER BY data ko sort karta hai — jaise Amazon par "Price: Low to High" ya "Price: High to Low" select karte ho. ASC = chhote se bade (A→Z, 1→100). DESC = bade se chhote (Z→A, 100→1). Multiple columns par sort kar sakte ho — "pehle department se sort karo, phir salary se." Bina ORDER BY ke SQL koi specific order guarantee nahi karta!

-- Sort by salary (ascending — default)
SELECT *
FROM employees
ORDER BY salary;
-- Sort by salary (descending — highest first)
SELECT *
FROM employees
ORDER BY salary DESC;
-- Sort by multiple columns
SELECT *
FROM employees
ORDER BY department ASC, salary DESC;

Q14: What is LIMIT (or TOP) clause?

Answer: LIMIT restricts the number of rows returned by a query. In MySQL it is LIMIT, in SQL Server it is TOP. It is commonly used with ORDER BY to get "Top N" results.

🎯 Explanation: LIMIT se bolo "sirf 5 rows chahiye" ya "sirf top 10 dikhao." ORDER BY ke saath use karo — "sabse zyada salary wale top 5 employees dikhao." Bina ORDER BY ke LIMIT random rows de sakta hai — meaningful results ke liye ORDER BY + LIMIT combo use karo. Pagination mein bhi LIMIT use hota hai — LIMIT 10 OFFSET 20 = page 3 ke results.

-- Top 5 highest paid employees (MySQL)
SELECT *
FROM employees
ORDER BY salary DESC
LIMIT 5;
-- Pagination: Skip first 10, get next 10 (page 2)
SELECT *
FROM employees
ORDER BY emp_id
LIMIT 10
OFFSET 10;

Q15: What are Aliases in SQL?

Answer: Aliases are temporary names given to tables or columns for the duration of a query. They make queries more readable and are created using the AS keyword (which is optional in most databases).

🎯 Explanation: Alias = nickname. Column ka naam bahut lamba hai ya calculated column hai — alias se chhota readable naam do. AVG(salary) AS avg_salary — result mein column header "avg_salary" dikhega. Table alias JOINs mein useful hai — FROM employees AS e likhne ke baad e.name likh sakte ho. AS optional hai lekin readability ke liye likho.

-- Column Alias
SELECT name, salary * 12 AS annual_salary
FROM employees;
-- Table Alias (useful in JOINs)
SELECT e.name, d.dept_name
FROM employees AS e
JOIN departments AS d
ON e.dept_id = d.dept_id;

Q16: What is GROUP BY clause?

Answer: GROUP BY groups rows that have the same values in specified columns into summary rows. It is used with aggregate functions (COUNT, SUM, AVG, MIN, MAX) to perform calculations on each group.

🎯 Explanation: GROUP BY data ko groups mein todta hai aur har group par aggregate function apply karta hai. "Har department mein kitne employees hain?" — GROUP BY department, COUNT(*). "Har city ki average salary?" — GROUP BY city, AVG(salary). Bina GROUP BY ke aggregate function poore table ka ek result dega. GROUP BY ke saath har group ka alag result milta hai.

-- Count employees per department
SELECT department, COUNT(*) AS emp_count
FROM employees
GROUP BY department;
-- Average salary per city
SELECT city, AVG(salary) AS avg_salary
FROM employees
GROUP BY city
ORDER BY avg_salary DESC;

Q17: What is NULL in SQL?

Answer: NULL represents a missing, unknown, or undefined value in SQL. It is NOT the same as zero (0) or an empty string (''). NULL means "no data exists." You cannot compare NULL using = or !=, you must use IS NULL or IS NOT NULL.

🎯 Explanation: NULL = "pata nahi" ya "data hai hi nahi." 0 = value hai, zero hai. '' = empty string hai, value hai (khali). NULL = koi value hi nahi hai. WHERE salary = NULL GALAT hai — kaam nahi karega. WHERE salary IS NULL SAHI hai. NULL + 5 = NULL (kuch bhi NULL ke saath calculate karo — result NULL). Bahut important concept hai — interviews mein NULL behavior puchha jaata hai!

-- Find employees with no email (NULL)
SELECT *
FROM employees
WHERE email IS NULL;
-- Find employees with email (NOT NULL)
SELECT *
FROM employees
WHERE email IS NOT NULL;
-- WRONG way (this won't work!)
SELECT *
FROM employees
WHERE email = NULL;
-- ❌ WRONG

Q18: What are IFNULL, COALESCE, and NULLIF?

Answer: IFNULL(value, replacement): Returns replacement if value is NULL (MySQL specific). COALESCE(val1, val2, ...): Returns the first non-NULL value from the list (standard SQL). NULLIF(val1, val2): Returns NULL if val1 equals val2, otherwise returns val1.

🎯 Explanation: IFNULL = "agar NULL hai toh yeh value de do." COALESCE = "pehli non-NULL value de do list mein se." NULLIF = "agar dono same hain toh NULL de do." Example: email NULL hai toh "N/A" dikhao — IFNULL(email, 'N/A'). Phone NULL hai toh email dikhao, woh bhi NULL toh "No Contact" — COALESCE(phone, email, 'No Contact'). Division mein denominator 0 hai toh NULL karo — NULLIF(denominator, 0).

-- IFNULL: Replace NULL with default value
SELECT name, IFNULL(email, 'N/A') AS email
FROM employees;
-- COALESCE: First non-NULL value
SELECT name, COALESCE(phone, email, 'No Contact') AS contact
FROM employees;
-- NULLIF: Return NULL if values are equal
SELECT salary / NULLIF(bonus, 0) AS ratio
FROM employees;

Q19: How does NULL behave with aggregate functions?

Answer: Most aggregate functions (SUM, AVG, MIN, MAX, COUNT(column)) ignore NULL values. COUNT(*) counts all rows including NULLs. COUNT(column) counts only non-NULL values in that column.

🎯 Explanation: Agar salary column mein values hain: 50000, NULL, 60000, NULL, 70000. SUM = 180000 (NULLs ignored). AVG = 60000 (180000/3 — sirf non-NULL values count). COUNT(*) = 5 (saari rows). COUNT(salary) = 3 (sirf non-NULL values). Yeh behavior jaanna zaroori hai — agar NULL ko 0 treat karna hai toh IFNULL/COALESCE use karo pehle.

-- COUNT(*) vs COUNT(column)
SELECT COUNT(*) AS total_rows, -- Counts ALL rows (incl NULL) COUNT(email) AS with_email,
-- Counts non-NULL emails only AVG(salary) AS avg_salary, -- Ignores NULLs in calculation AVG(IFNULL(salary, 0)) AS avg_with_zero -- Treats NULL as 0 FROM employees;

Q20: What is a Primary Key?

Answer: A Primary Key is a column (or combination of columns) that uniquely identifies each row in a table. It cannot contain NULL values and must have unique values. A table can have only ONE Primary Key.

🎯 Explanation: Primary Key = Aadhaar Number jaisa — har person ka unique, kisi ka duplicate nahi, khali nahi ho sakta. Table mein har row ko identify karne ke liye ek unique column chahiye — woh Primary Key hai. emp_id, order_id, roll_no — yeh common Primary Keys hain. Bina Primary Key ke rows uniquely identify nahi ho paati — data integrity kharab hoti hai.

CREATE TABLE employees ( emp_id INT PRIMARY KEY, name VARCHAR(100), salary DECIMAL(10,2) );
-- Auto Increment Primary Key
CREATE TABLE employees ( emp_id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(100) );

Q21: What is a Foreign Key?

Answer: A Foreign Key is a column in one table that references the Primary Key of another table. It creates a link between two tables and enforces referential integrity — ensuring that values in the Foreign Key column must exist in the referenced Primary Key column.

🎯 Explanation: Foreign Key ek reference hai doosri table ki taraf. Jaise employees table mein dept_id column hai — yeh departments table ke dept_id (Primary Key) ko point karta hai. Isse dono tables connected hain. Foreign Key ensure karta hai ki tum koi aisi dept_id nahi daal sakte employees mein jo departments table mein exist nahi karti — data consistency maintain hoti hai.

CREATE TABLE departments ( dept_id INT PRIMARY KEY, dept_name VARCHAR(100) );
CREATE TABLE employees ( emp_id INT PRIMARY KEY, name VARCHAR(100), dept_id INT, FOREIGN KEY (dept_id) REFERENCES departments(dept_id) );

Q22: What are the different types of Constraints in SQL?

Answer: Constraints are rules applied to columns to enforce data integrity. They restrict the type of data that can be inserted into a table.

Constraint Purpose Example
PRIMARY KEY Unique + NOT NULL identifier emp_id INT PRIMARY KEY
FOREIGN KEY Reference to another table's PK FOREIGN KEY (dept_id) REFERENCES departments(dept_id)
NOT NULL Column cannot be NULL name VARCHAR(100) NOT NULL
UNIQUE All values must be different email VARCHAR(255) UNIQUE
CHECK Value must satisfy a condition CHECK (salary > 0)
DEFAULT Default value if none provided status VARCHAR(20) DEFAULT 'Active'

🎯 Explanation: Constraints rules hain jo data quality maintain karte hain. NOT NULL = yeh column khali nahi reh sakta. UNIQUE = duplicate values nahi aa sakti (email unique hona chahiye). CHECK = condition satisfy honi chahiye (salary negative nahi ho sakti). DEFAULT = agar value nahi di toh yeh default lagegi. Yeh sab table create karte waqt define karte hain.

Q23: What is the difference between UNIQUE and PRIMARY KEY?

Answer: Both ensure uniqueness, but PRIMARY KEY also enforces NOT NULL and a table can have only ONE Primary Key. UNIQUE allows one NULL value and a table can have multiple UNIQUE constraints.

Feature PRIMARY KEY UNIQUE
NULL allowed? ❌ No ✅ One NULL allowed
Per table count Only ONE Multiple allowed
Creates index? Clustered index (default) Non-clustered index

🎯 Explanation: PRIMARY KEY = Aadhaar (ek hi ho sakta hai, khali nahi ho sakta). UNIQUE = phone number (duplicate nahi lekin NULL ho sakta hai — kisi ke paas phone nahi hai). Ek table mein ek hi Primary Key lekin multiple UNIQUE columns ho sakte hain (email UNIQUE, phone UNIQUE, PAN UNIQUE).

Q24: What is AUTO_INCREMENT?

Answer: AUTO_INCREMENT automatically generates a unique sequential number for a column (usually the Primary Key) whenever a new row is inserted. You don't need to manually provide the value — the database handles it.

🎯 Explanation: AUTO_INCREMENT = automatic numbering system. Pehla employee insert karo — emp_id automatically 1 milega. Doosra insert karo — 2 milega. Tumhe manually ID dene ki zaroorat nahi. Agar row delete karo (ID 3) — next insert 4 hoga, 3 reuse nahi hoga. Default start 1 se hota hai lekin change kar sakte ho.

CREATE TABLE employees ( emp_id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(100), salary DECIMAL(10,2) );
-- Insert without providing emp_id

INSERT INTO employees (name, salary)
VALUES ('Amit', 50000);
-- emp_id = 1 (auto)

INSERT INTO employees (name, salary)
VALUES ('Priya', 60000);
-- emp_id = 2 (auto)

Q25: What are the common SQL Data Types?

Answer: SQL data types define what kind of value a column can hold. Major categories: Numeric, String, Date/Time.

Category Data Type Use Case
Numeric INT Whole numbers (emp_id, age, qty)
DECIMAL(p,s) Exact decimals (salary, price)
FLOAT / DOUBLE Approximate decimals (scientific data)
String VARCHAR(n) Variable-length text (name, email)
CHAR(n) Fixed-length text (gender 'M'/'F', country code)
TEXT Large text (description, comments)
Date/Time DATE Date only (YYYY-MM-DD)
DATETIME Date + Time
TIMESTAMP Auto-updated date/time (created_at)

🎯 Explanation: VARCHAR vs CHAR — VARCHAR variable length hai (naam 4 character ka ho ya 20 ka — space accordingly use hoga). CHAR fixed length hai (CHAR(10) mein 4 character ka naam bhi 10 character space lega). VARCHAR zyada common hai. DECIMAL(10,2) matlab total 10 digits mein se 2 decimal ke baad — 12345678.90. Financial data ke liye DECIMAL use karo, FLOAT nahi (precision loss hota hai).

Q26: How do you CREATE a table?

Answer: The CREATE TABLE statement defines a new table with column names, data types, and constraints.

🎯 Explanation: CREATE TABLE se nayi table banate hain — columns define karo with data types aur constraints. Socho furniture order kar rahe ho — "ek table chahiye jisme 5 columns hon: ID (number, unique), Name (text, required), Email (text, unique), Salary (decimal), Join Date (date)." SQL mein yehi likho.

CREATE TABLE employees ( emp_id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(100) NOT NULL, email VARCHAR(255) UNIQUE, salary DECIMAL(10,2) DEFAULT 0, department VARCHAR(50), join_date DATE, is_active BOOLEAN DEFAULT TRUE );

Q27: How do you INSERT data into a table?

Answer: The INSERT INTO statement adds new rows to a table. You can insert a single row, multiple rows, or insert data from another table.

🎯 Explanation: INSERT INTO se data daalta hain table mein. Do tarike hain — (1) columns specify karo aur values do. (2) Saare columns ki values do order mein. Multiple rows ek statement mein insert kar sakte ho — comma se separate karo. INSERT + SELECT se ek table ka data doosri table mein copy kar sakte ho.

-- Insert single row (specifying columns)

INSERT INTO employees (name, salary, department)
VALUES ('Amit Kumar', 55000, 'Sales');
-- Insert multiple rows

INSERT INTO employees (name, salary, department)
VALUES ('Priya Patel', 62000, 'IT'), ('Rahul Verma', 48000, 'HR'), ('Neha Gupta', 71000, 'Marketing');
-- Insert from another table INSERT INTO employees_backup
SELECT *
FROM employees;

Q28: How do you UPDATE and DELETE data?

Answer: UPDATE modifies existing data in a table based on conditions. DELETE removes rows from a table. Both use WHERE clause to target specific rows — without WHERE, they affect ALL rows.

⚡ Warning: UPDATE aur DELETE mein WHERE clause bhoolna SABSE DANGEROUS mistake hai. Bina WHERE ke UPDATE saari rows update kar dega aur DELETE saari rows delete kar dega. Hamesha pehle SELECT se verify karo ki WHERE sahi rows target kar raha hai — phir UPDATE/DELETE run karo!

-- UPDATE: Modify existing data

UPDATE employees
SET salary = 65000
WHERE emp_id = 101;
-- UPDATE multiple columns

UPDATE employees
SET salary = 70000, department = 'Marketing'
WHERE emp_id = 102;
-- DELETE: Remove specific rows DELETE FROM employees WHERE emp_id = 105;
-- ❌ DANGEROUS: Without WHERE — deletes ALL rows! DELETE FROM employees;
-- ALL rows gone!

Q29: What are Aggregate Functions in SQL?

Answer: Aggregate functions perform calculations on a set of values and return a single result. Main aggregate functions: COUNT, SUM, AVG, MIN, MAX. They are commonly used with GROUP BY.

Function Purpose Example
COUNT() Count rows COUNT(*), COUNT(email)
SUM() Total of values SUM(salary)
AVG() Average of values AVG(salary)
MIN() Smallest value MIN(salary)
MAX() Largest value MAX(salary)

🎯 Explanation: Aggregate functions multiple rows ko ek result mein convert karte hain. "Total salary kitni hai?" — SUM. "Average salary?" — AVG. "Kitne employees hain?" — COUNT. "Sabse kam salary?" — MIN. "Sabse zyada?" — MAX. GROUP BY ke saath use karo — "har department ki average salary?" GROUP BY department + AVG(salary).

SELECT COUNT(*) AS total_employees, SUM(salary) AS total_salary,
AVG(salary) AS avg_salary, MIN(salary) AS min_salary, MAX(salary) AS max_salary
FROM employees;
-- With GROUP BY
SELECT department, COUNT(*) AS emp_count, AVG(salary) AS avg_salary
FROM employees
GROUP BY department;

Q30: What is the correct order of SQL clauses in a SELECT statement?

Answer: SQL clauses must be written in a specific order. If the order is wrong, the query will throw a syntax error.

CORRECT
ORDER (Writing Order): ════════════════════════════════ 1. SELECT → Kaunse columns chahiye 2.
FROM → Kaunsi table se 3.
JOIN → Doosri table jodna ho toh 4.
WHERE → Rows filter (BEFORE grouping) 5.
GROUP BY → Rows
group karna 6.
HAVING → Groups filter (AFTER grouping) 7.
ORDER BY → Result sort karna 8.
LIMIT → Result rows
limit karna EXECUTION
ORDER (Database internally): ═══════════════════════════════════════ 1.
FROM → Table identify 2.
JOIN → Tables combine 3.
WHERE → Rows filter 4.
GROUP BY → Groups create 5.
HAVING → Groups filter 6. SELECT → Columns select 7.
ORDER BY → Sort 8.
LIMIT →
Limit rows

🎯 Explanation: Likhne ka order aur execute hone ka order ALAG hai! Hum SELECT pehle likhte hain lekin database pehle FROM execute karta hai (table dhundhta hai), phir WHERE (filter), phir GROUP BY (group), phir HAVING (group filter), phir SELECT (columns choose), phir ORDER BY (sort), phir LIMIT (restrict). Yeh isliye important hai kyunki WHERE mein alias use nahi kar sakte (SELECT baad mein execute hota hai) lekin ORDER BY mein kar sakte ho (SELECT pehle execute ho chuka hai). Interviews mein yeh execution order puchha jaata hai!

-- Complete query with all clauses in correct order
SELECT department, AVG(salary) AS avg_salary
FROM employees
WHERE is_active = TRUE
GROUP BY department
HAVING AVG(salary) > 50000
ORDER BY avg_salary DESC
LIMIT 5;

Quick Revision — 30 Questions at a Glance

Q# Question One-Line Answer
1What is SQL?Language to manage & query relational databases
2Types of SQL commands?DDL, DML, DQL, DCL, TCL
3SQL vs MySQL?SQL = language, MySQL = software/RDBMS
4Database & RDBMS?Organized data + software that manages it in tables
5DROP vs DELETE vs TRUNCATE?DROP=table remove, DELETE=rows remove, TRUNCATE=all data remove
6Retrieve all data?SELECT * FROM table_name
7WHERE clause?Filter rows based on conditions
8WHERE vs HAVING?WHERE=before grouping, HAVING=after grouping
9DISTINCT?Returns unique values, removes duplicates
10WHERE operators?=, !=, >, <, BETWEEN, IN, LIKE, IS NULL, AND/OR
11LIKE & wildcards?% = any chars, _ = one char
12IN vs BETWEEN?IN=discrete list, BETWEEN=continuous range
13ORDER BY?Sort results ASC/DESC
14LIMIT?Restrict number of rows returned
15Aliases?Temporary names using AS keyword
16GROUP BY?Group rows + apply aggregate functions
17What is NULL?Missing/unknown value, not 0 or empty string
18IFNULL, COALESCE, NULLIF?NULL handling functions
19NULL with aggregates?Aggregates ignore NULLs, COUNT(*) counts all
20Primary Key?Unique + NOT NULL identifier, one per table
21Foreign Key?References another table's Primary Key
22Constraints?PK, FK, NOT NULL, UNIQUE, CHECK, DEFAULT
23UNIQUE vs PRIMARY KEY?PK=one per table+no NULL, UNIQUE=multiple+allows NULL
24AUTO_INCREMENT?Auto-generates sequential unique IDs
25Data Types?INT, VARCHAR, DECIMAL, DATE, BOOLEAN, TEXT
26CREATE TABLE?Define new table with columns, types, constraints
27INSERT data?INSERT INTO table (cols) VALUES (vals)
28UPDATE & DELETE?Modify/remove rows — always use WHERE!
29Aggregate Functions?COUNT, SUM, AVG, MIN, MAX
30Clause order?SELECT→FROM→JOIN→WHERE→GROUP BY→HAVING→ORDER BY→LIMIT

Next: SQL Interview Questions — Intermediate (30 Questions)

Agle blog mein cover karenge: JOINs (INNER, LEFT, RIGHT, FULL, SELF, CROSS), Subqueries, UNION/UNION ALL, EXISTS, CASE WHEN, Views, Indexes, INSERT+SELECT, UPDATE with JOIN — sab detailed answers aur queries ke saath. Yeh 1-2 years experience level ke liye hai. Basic SQL Interview Questions Data Insights par available hai.

Happy Learning & Keep Querying! 🚀

👤
Jatin Kumar
Data Analyst & Educator

Python, SQL, Power BI aur Excel mein practical tutorials likhta hoon — taaki data analytics seekhna aasan ho. Portfolio: jatinanalytics.co.in

Portfolio LinkedIn GitHub Kaggle All Articles
Share:

💬 Comments (0)

Spam/links allowed nahi hain — respectful comments welcome!

Loading comments...

Was this article helpful?
Next Article Top 30 Intermediate SQL Interview Questions And Answers