SQL interview questions
give me interview question for sql and also some basic
practical that possible to come in interview with ans
Here’s a helpful list of commonly asked SQL interview questions along with basic
practical tasks and their answers. These questions cover both theory and hands-on
SQL skills that are often tested in interviews — especially for internships, fresher
roles, and junior developer positions.
✅ Theory-Based Interview Questions (with Answers)
1. What is SQL?
Answer: SQL (Structured Query Language) is used to communicate with
relational databases to perform operations like insert, update, delete, and
retrieve data.
2. What are the different types of SQL commands?
Answer:
DDL (Data Definition Language): CREATE , ALTER , DROP
DML (Data Manipulation Language): INSERT , UPDATE , DELETE
DQL (Data Query Language): SELECT
DCL (Data Control Language): GRANT , REVOKE
TCL (Transaction Control Language): COMMIT , ROLLBACK , SAVEPOINT
3. What is the difference between WHERE and HAVING ?
Answer: WHERE filters rows before grouping; HAVING filters groups after
GROUP BY .
4. What is a primary key?
Answer: A column (or set of columns) that uniquely identifies each row in a
table. It cannot have NULL values.
5. What is the difference between INNER JOIN , LEFT JOIN , RIGHT JOIN , and
FULL OUTER JOIN ?
Answer:
INNER JOIN : Returns matching rows from both tables.
LEFT JOIN : Returns all rows from left table + matching from right.
RIGHT JOIN : All rows from right table + matching from left.
1/16
FULL JOIN : All rows from both tables, with NULLs where no match.
6. What is normalization?
Answer: Process of organizing data to reduce redundancy and improve data
integrity (1NF, 2NF, 3NF, etc.).
7. What is a foreign key?
Answer: A column in one table that links to the primary key of another table
to maintain referential integrity.
8. What is the difference between DELETE , TRUNCATE , and DROP ?
Answer:
DELETE : Removes rows based on condition (can rollback).
TRUNCATE : Removes all rows quickly (cannot rollback in some DBs).
DROP : Deletes entire table structure and data.
🧪 Basic SQL Practical Tasks (with Answers)
Assume we have this table:
sql
CREATE TABLE Employees (
id INT PRIMARY KEY,
name VARCHAR(50),
department VARCHAR(50),
salary INT
);
INSERT INTO Employees (id, name, department, salary) VALUES
(1, 'Alice', 'HR', 40000),
(2, 'Bob', 'Sales', 50000),
(3, 'Charlie', 'HR', 45000),
(4, 'David', 'IT', 60000),
(5, 'Eva', 'Sales', 55000);
1. Select all employees.
sql
2/16
SELECT * FROM Employees;
2. Find employees in the HR department.
sql
SELECT * FROM Employees WHERE department = 'HR';
3. Find employees with salary greater than 45000.
sql
SELECT * FROM Employees WHERE salary > 45000;
4. Increase salary by 10% for Sales department.
sql
UPDATE Employees SET salary = salary * 1.10 WHERE department = 'Sales';
5. Count how many employees are in each department.
sql
SELECT department, COUNT(*) AS total_employees
FROM Employees
GROUP BY department;
6. Get the average salary of employees.
sql
SELECT AVG(salary) AS average_salary FROM Employees;
3/16
7. Find the highest salary.
sql
SELECT MAX(salary) AS highest_salary FROM Employees;
8. Delete employees with salary less than 45000.
sql
DELETE FROM Employees WHERE salary < 45000;
9. Rename column name to full_name .
sql
-- Syntax varies by DBMS (example for MySQL):
ALTER TABLE Employees CHANGE name full_name VARCHAR(50);
10. Join Example (Employees + Departments)
Assume another table:
sql
CREATE TABLE Departments (
department_name VARCHAR(50),
manager VARCHAR(50)
);
INSERT INTO Departments VALUES
('HR', 'Anita'),
('Sales', 'Brian'),
('IT', 'Charles');
Join to get employee name with manager name:
sql
4/16
SELECT [Link], [Link], [Link]
FROM Employees E
JOIN Departments D
ON [Link] = D.department_name;
🧠 Tip for Interviews:
Practice writing queries without autocomplete.
Learn to optimize queries: use indexes, avoid SELECT *, etc.
Know your basic subqueries, window functions, and constraints.
If you want I can also generate a SQL test set with questions and allow you to
practice and check answers interactively. Want that too?
also for nodejs basic practical and question ask in
interview
Sure! Here's a complete list of [Link] interview questions (theory + practical) that
are commonly asked in interviews for interns, freshers, or junior developers, along
with code examples to help you prepare effectively.
✅ [Link] Interview Questions (with Answers)
📘 Basic Theory Questions
1. What is [Link]?
Answer: [Link] is a JavaScript runtime built on Chrome's V8 engine that
allows you to run JavaScript on the server side.
2. What is the difference between [Link] and JavaScript?
Answer: JavaScript runs in the browser; [Link] runs JavaScript outside the
browser (e.g., on the server).
3. What is npm?
Answer: npm (Node Package Manager) is the default package manager for
[Link] used to install libraries or packages.
5/16
4. What is a callback function?
Answer: A function passed into another function to be executed later
(usually after an async task).
5. What is event-driven programming in [Link]?
Answer: [Link] uses events to handle asynchronous operations using the
EventEmitter module.
6. What is the use of require() in [Link]?
Answer: It imports modules, JSON, or local files into a [Link] file.
7. Difference between synchronous and asynchronous code?
Answer:
Synchronous: Code is executed line by line.
Asynchronous: Next line can execute before previous completes (e.g.,
file I/O, HTTP calls).
8. What is middleware in [Link]?
Answer: Middleware functions have access to the request, response, and
next function in the request-response cycle. Common in [Link].
9. What is the difference between [Link]() , setImmediate() and
setTimeout() ?
Answer:
[Link]() – Runs after the current operation.
setImmediate() – Runs after the current event loop.
setTimeout() – Runs after a set delay.
10. How does [Link] handle concurrency if it's single-threaded?
Answer: It uses an event loop and non-blocking I/O to handle multiple
operations concurrently.
🧪 [Link] Basic Practical Tasks (with Answers)
🔹 1. Create a simple HTTP server in [Link]
js
const http = require('http');
const server = [Link]((req, res) => {
6/16
[Link]('Hello, World!');
[Link]();
});
[Link](3000, () => {
[Link]('Server running at [Link]
});
🔹 2. Read a file using fs module
js
const fs = require('fs');
[Link]('[Link]', 'utf8', (err, data) => {
if (err) throw err;
[Link](data);
});
🔹 3. Create a basic [Link] API
js
const express = require('express');
const app = express();
[Link]('/', (req, res) => {
[Link]('Welcome to Express!');
});
[Link](3000, () => {
[Link]('Server running on port 3000');
});
🔹 4. POST request with JSON body (Express + middleware)
js
7/16
const express = require('express');
const app = express();
[Link]([Link]()); // middleware
[Link]('/data', (req, res) => {
const received = [Link];
[Link]({ message: 'Data received', received });
});
[Link](3000);
🔹 5. Create your own module and import it
[Link]
js
function add(a, b) {
return a + b;
}
[Link] = { add };
[Link]
js
const math = require('./math');
[Link]([Link](5, 3)); // Output: 8
🔹 6. Create a basic middleware in Express
js
function logger(req, res, next) {
[Link](`${[Link]} ${[Link]}`);
next();
}
8/16
[Link](logger);
🔹 7. Environment variables using dotenv
bash
# .env
PORT=4000
js
require('dotenv').config();
[Link]([Link]);
🔹 8. Make an HTTP request (GET) using axios
js
const axios = require('axios');
[Link]('[Link]
.then(response => [Link]([Link]))
.catch(error => [Link](error));
🔹 9. Async/Await example
js
const fs = require('fs/promises');
async function readFile() {
try {
const data = await [Link]('[Link]', 'utf8');
[Link](data);
} catch (err) {
9/16
[Link](err);
}
}
readFile();
🔹 10. Simple error handling
js
[Link]('/error', (req, res) => {
try {
throw new Error('Something went wrong!');
} catch (err) {
[Link](500).send([Link]);
}
});
🧠 Bonus Tips for Interview
Know the difference between CommonJS ( require ) and ES Modules ( import )
Practice using async/await vs callbacks
Be able to explain how the event loop works
Understand error handling, routing, and middleware flow
Be ready to write a simple API live (GET, POST)
Would you like a practice test or mini project idea using [Link] and MongoDB or
Express? I can give you a full task with solution if needed.
Know the difference between CommonJS (require) and
ES Modules (import)
Practice using async/await vs callbacks
Be able to explain how the event loop works
10/16
Understand error handling, routing, and middleware
flow
Be ready to write a simple API live (GET, POST)
also give me ans of this
Great! Below are clear answers and code examples for each of the important
[Link] interview areas you listed — explained in a way that makes them easy to
understand and apply in interviews.
✅ 1. Difference Between CommonJS ( require ) and ES Modules
( import )
CommonJS
Feature ( require ) ES Modules ( import )
Syntax const fs = import fs from 'fs'
require('fs')
File Extension .js (default) .mjs or "type":
"module" in
[Link]
Synchronous Yes Asynchronous
Used In [Link] (default) [Link] (modern),
Browsers
Exports [Link] = export default
{...} {...} or export {}
CommonJS example:
js
// [Link]
function add(a, b) {
return a + b;
}
11/16
[Link] = { add };
// [Link]
const math = require('./math');
[Link]([Link](2, 3));
ES Module example:
js
// [Link]
export function add(a, b) {
return a + b;
}
// [Link]
import { add } from './[Link]';
[Link](add(2, 3));
✅ 2. Async/Await vs Callbacks
⏱️ Callback Example
js
const fs = require('fs');
[Link]('[Link]', 'utf8', (err, data) => {
if (err) return [Link](err);
[Link](data);
});
⏳ Async/Await Example
js
const fs = require('fs/promises');
async function readFile() {
try {
const data = await [Link]('[Link]', 'utf8');
12/16
[Link](data);
} catch (err) {
[Link](err);
}
}
readFile();
✅ Difference:
Callback: Pass a function to run after an async operation.
Async/Await: Makes async code look synchronous, easier to read, better error
handling with try...catch .
✅ 3. How the Event Loop Works in [Link]
Short Answer (for interviews):
[Link] is single-threaded but uses an event loop to handle non-blocking I/O
operations. It puts async tasks in a queue and processes them after the current call
stack is empty.
Example:
js
[Link]('Start');
setTimeout(() => {
[Link]('Timeout');
}, 0);
[Link]().then(() => [Link]('Promise'));
[Link]('End');
Output:
sql
Start
End
13/16
Promise
Timeout
Why?
Promise (microtask) is handled before setTimeout (macrotask) in the event
loop.
✅ 4. Error Handling, Routing, and Middleware Flow (Express)
🔸 Basic Error Handling Example
js
[Link]('/error', (req, res, next) => {
const err = new Error('Something went wrong');
next(err); // send to error middleware
});
[Link]((err, req, res, next) => {
[Link](500).json({ error: [Link] });
});
🔸 Routing Example
js
[Link]('/users', (req, res) => {
[Link]('User list');
});
🔸 Middleware Flow Example
js
[Link]((req, res, next) => {
[Link](`${[Link]} ${[Link]}`);
next(); // pass control to next middleware or route
});
🔁 Flow:
14/16
Request → Middleware → Route Handler → (Optional) Error Handler
✅ 5. Live API Example: Simple GET and POST
js
const express = require('express');
const app = express();
[Link]([Link]()); // middleware to parse JSON
// GET route
[Link]('/greet', (req, res) => {
[Link]('Hello, Developer!');
});
// POST route
[Link]('/submit', (req, res) => {
const { name } = [Link];
[Link]({ message: `Hello, ${name}` });
});
// Start server
[Link](3000, () => {
[Link]('Server running on [Link]
});
Test with Postman or Curl:
bash
curl -X POST [Link] -H "Content-Type: application/json" -d
'{"name":"Alice"}'
🎁 Bonus Tip for Interviews:
Prepare to explain each part of your API code, not just write it.
If they ask, answer like this:
GET: Used to fetch data.
15/16
POST: Used to send data.
[Link]() : Parses incoming JSON data.
Error Middleware: Catches and handles all errors centrally.
Would you like me to generate a real-world mini project (like Todo app API) using
Express and MongoDB for practice next?
Do you like this personality?
16/16