0% found this document useful (0 votes)
31 views11 pages

SQL Technical Interview Q&A

The document provides a comprehensive overview of SQL technical interview questions and answers, covering fundamental concepts such as databases, SQL, MySQL, and various SQL commands. It includes explanations of key terms like primary keys, foreign keys, normalization, and joins, along with practical examples of SQL queries. Additionally, it outlines common SQL commands and operations for managing data in databases.

Uploaded by

ramprasath220922
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views11 pages

SQL Technical Interview Q&A

The document provides a comprehensive overview of SQL technical interview questions and answers, covering fundamental concepts such as databases, SQL, MySQL, and various SQL commands. It includes explanations of key terms like primary keys, foreign keys, normalization, and joins, along with practical examples of SQL queries. Additionally, it outlines common SQL commands and operations for managing data in databases.

Uploaded by

ramprasath220922
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

SQL

Technical
Interview Q&A
and
Sample Queries
SQL Technical Interview Question and Answers:
1. What is Database?
A database is a place to store and manage data. It holds information in an organized way
Example: A school database stores student names, marks, and attendance.
2. What is SQL?
SQL (Structured Query Language) is a language used to read, write, and manage data in a
database.
3. What is MySQL?
MySQL is a popular database software that uses SQL to manage databases. It is free and
open-source.
4. What is DBMS?
A DBMS (Database Management System) is software that stores and retrieves data. It doesn’t
necessarily use relational tables.
Example DBMS: MS Access, file system
5. What Is RDBMS?
RDBMS (Relational DBMS) is a type of DBMS that stores data in tables and allows relations
using keys.
Example: MySQL, PostgreSQL
6. Difference between SQL and MySQL
SQL MySQL

Language Software

Used to query data Uses SQL to manage data

Standard Specific tool

7. Difference between DBMS and RDBMS


DBMS RDBMS

No relation between data Uses keys for relations

Slower Faster with large data

No support for normalization Supports normalization

8. What are tables and fields?


A table stores data in rows and columns.
A field is a column in a table.
9. What are the types of SQL commands?
DDL (Data Definition): CREATE, ,ALTER, TRUNCATE, DROP
DML (Data Manipulation): SELECT, INSERT, UPDATE, DELETE
DCL (Data Control): GRANT, REVOKE
TCL (Transaction Control): COMMIT, ROLLBACK
DQL (Data Query Language): SELECT
10. What are the constraints in SQL?
Constraints define rules for data in a table
11. What are Constraints?
• NOT NULL
• UNIQUE
• PRIMARY KEY
• FOREIGN KEY
• CHECK
• DEFAULT
12. What is primary key?
A primary key uniquely identifies each record in a table. It cannot be null and must be unique.
CREATE TABLE students (
id INT PRIMARY KEY,
name VARCHAR(50)
);
13. What is foreign key?
A foreign key links one table to another. It refers to the primary key of another table.
CREATE TABLE orders (
order_id INT,
student_id INT,
FOREIGN KEY (student_id) REFERENCES students(id)
);
14. What is unique constraint?
A unique constraint ensures that all values in a column are different. Unlike primary key, it can
have one null value.
CREATE TABLE users (
email VARCHAR(100) UNIQUE
);
15. What is default constraint?
A default constraint sets a default value for a column when no value is provided during insert.
CREATE TABLE products (
name VARCHAR(100),
status VARCHAR(20) DEFAULT 'In Stock'
);
16. What is delete, drop and truncate?
• DELETE removes rows with a WHERE condition.
• TRUNCATE removes all rows but keeps table structure.
• DROP deletes the table completely.
DELETE FROM students WHERE id = 1;
TRUNCATE TABLE students;
DROP TABLE students;
17. What is where clause?
The WHERE clause filters records based on a condition.
SELECT * FROM students WHERE age > 18;
18. What is groupby in SQL?
GROUP BY groups rows that have the same values in specified columns, often used with
aggregate functions. SELECT department, COUNT(*) FROM employees GROUP BY department;
19. What is having clause?
HAVING is like WHERE but used with GROUP BY to filter groups based on aggregate
conditions.
SELECT department, COUNT(*) FROM employees
GROUP BY department HAVING COUNT(*) > 5;
20. What is orderby in SQL?
ORDER BY is used to sort the result set in ascending (default) or descending order.
SELECT * FROM students ORDER BY age DESC;
21. Difference Between WHERE and HAVING clause.

22. What is char and varchar?


CHAR(n) stores fixed-length strings.
VARCHAR(n) stores variable-length strings, saving space.
name CHAR(10), address VARCHAR(100)
23. What is subquery?
A subquery is a query inside another SQL query, usually used in WHERE or SELECT
SELECT name FROM students
WHERE id IN
(SELECT student_id FROM marks WHERE score > 80);
24. What is nested query?
A nested query is the same as a subquery. It is executed first and its result is used in the outer
query.
SELECT * FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);
25. What is select in SQL?
SELECT is used to retrieve data from one or more tables.
SELECT name, age FROM students;
26. Alter statement
The ALTER statement is used to change the structure of an existing table. You can:
• Add a new column
• Modify an existing column
• Delete a column
• Rename a column or table
• Add/remove constraints
27. UPDATE statement
The UPDATE statement is used to change existing data in one or more rows of a table.
28. What is CTE?
A CTE (Common Table Expression) is a temporary result set defined using WITH, used to
simplify complex queries.
WITH TopStudents AS (
SELECT name, marks FROM students WHERE marks > 90
)
SELECT * FROM TopStudents;
29. What is view in SQL?
A view is a virtual table created from a query. It does not store data but shows results
dynamically.
CREATE VIEW StudentView AS
SELECT name, age FROM students;
30. What is stored procedure?
A stored procedure is a set of SQL statements saved in the database and executed as a
program.
CREATE PROCEDURE GetAllStudents()
BEGIN
SELECT * FROM students;
END;
31. What is trigger in SQL?
A trigger is an automatic action that runs in response to certain events like INSERT, UPDATE, or
DELETE.
CREATE TRIGGER before_insert_students
BEFORE INSERT ON students
FOR EACH ROW
SET NEW.created_at = NOW();
32. What is cursor?
A cursor is used to process each row returned by a query one by one. It is used in stored
procedures.
DECLARE student_cursor CURSOR FOR SELECT name FROM students;
33. What is normalization?
Normalization is the process of organizing data to reduce redundancy and improve data
integrity.
34. What are the types of normalization?
• First Normal Form (1NF)
• Second Normal Form (2NF)
• Third Normal Form (3NF)
• Boyce-Codd Normal Form (BCNF)
• Fourth Normal Form (4NF)
• Fifth Normal Form (5NF) or Project-Join Normal Form (PJNF)
35. What is denormalization?
Denormalization is the process of adding redundancy to improve read performance by
combining tables.
36. What is union and union all?
UNION combines results of two queries and removes duplicates.
UNION ALL includes all results with duplicates.
SELECT name FROM students
UNION
SELECT name FROM teachers;
37. What is join?
JOIN is used to combine rows from two or more tables based on a related column.
SELECT * FROM students
JOIN marks ON students.id = marks.student_id;
38. What are the types of join and explain?
• INNER JOIN: Returns matching rows.
• LEFT JOIN: All from left + matching from right.
• RIGHT JOIN: All from right + matching from left.
• FULL OUTER JOIN: All rows from both tables.
39. What are the ACID properties in SQL?
• Atomicity: All or nothing.
• Consistency: Data is valid before and after transaction.
• Isolation: Transactions run independently.
• Durability: Data is saved even after failure.
40. What is commit and rollback?
COMMIT saves changes to the database.
ROLLBACK undoes changes since the last COMMIT.
BEGIN;
DELETE FROM students WHERE id=3;
ROLLBACK;
41. How many aggregate functions are available in SQL?
• AVG()
• COUNT()
• MAX()
• MIN()
• SUM()
• FIRST()
• LAST()
• Length()
• Upper()
• Lower()
• Replace()
42. What is an ALIAS command?
Alias renames columns or tables temporarily for better readability.
SELECT name AS student_name FROM students;
43. What is schema in SQL ?
A schema is the structure that contains tables, views, procedures, etc., within a database.
44. Relationships in SQL?
• One-to-One: One record in a table is related to one record in another table.
• One-to-Many: One record in a table is related to multiple records in another table.
• Many-to-Many: Multiple records in one table are related to multiple records in another
table (usually through a junction table).
45. What is clustered and non-clustered index?
Clustered Index: Sorts and stores data physically in table order.
Non-Clustered Index: Creates a separate structure for indexing.
46. What is purpose of distinct in keyword?
DISTINCT removes duplicate rows from result.
SELECT DISTINCT department FROM employees;
47. What is the difference between BETWEEN and IN operator?
BETWEEN filters within a range.
IN filters with a list of specific values.
WHERE age BETWEEN 18 AND 25 WHERE name IN ('Ram', 'Raj', 'Ravi')
48. What is the difference between IS NULL and = NULL?
Use IS NULL to check for null values. = NULL does not work.
SELECT * FROM users WHERE email IS NULL;
49. What is a Self JOIN?
A Self JOIN is a regular join where a table is joined with itself.
SELECT A.name, B.name FROM employees A, employees B
WHERE A.manager_id = B.id;
50. What is a CASE statement in SQL
Acts like an IF-ELSE condition in SQL.
SELECT name,
CASE
WHEN marks >= 50 THEN 'Pass'
ELSE 'Fail'
END AS result
FROM students;
51. How to find duplicate records in a table?
Using GROUP BY and HAVING.
SELECT name, COUNT(*) FROM students GROUP BY name HAVING COUNT(*) > 1;

SQL Query Question and answer


1. Common SQL Command:
Show databases;
Use database;
Show database;
Describe table_name;
2. Create Data base
Create database student_management;

3. Create table
CREATE TABLE departments (
dept_id INT PRIMARY KEY,
dept_name VARCHAR(50) NOT NULL UNIQUE
);
CREATE TABLE students (
student_id INT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
age INT CHECK (age >= 17),
gender VARCHAR(10),
dept_id INT,
CONSTRAINT fk_dept FOREIGN KEY (dept_id) REFERENCES departments(dept_id)
);

CREATE TABLE marks (


mark_id INT PRIMARY KEY,
student_id INT,
subject VARCHAR(50),
score INT CHECK (score BETWEEN 0 AND 100),
FOREIGN KEY (student_id) REFERENCES students(student_id)
);
4. Inserting Values
Departments
INSERT INTO departments VALUES (1, 'Computer Science');
INSERT INTO departments VALUES (2, 'Mechanical');
INSERT INTO departments VALUES (3, 'Electrical');
students
INSERT INTO students VALUES (101, 'Ram', 20, 'Male', 1);
INSERT INTO students VALUES (102, 'Anu', 19, 'Female', 2);
INSERT INTO students VALUES (103, 'Karthik', 21, 'Male', 1);
Marks
INSERT INTO marks VALUES (1, 101, 'Maths', 85);
INSERT INTO marks VALUES (2, 101, 'English', 78);
INSERT INTO marks VALUES (3, 102, 'Maths', 88);
INSERT INTO marks VALUES (4, 103, 'Physics', 90);
5. Alter statement
ALTER TABLE students ADD email VARCHAR(100);
ALTER TABLE students MODIFY email VARCHAR(150);
ALTER TABLE students CHANGE email student_email VARCHAR(100);
ALTER TABLE students DROP COLUMN gender;
ALTER TABLE students RENAME TO student_info;
6. UPDATE statement
1. Update a single row
UPDATE students
SET email = '[email protected]'
WHERE stud_id = 101;
2. Update multiple columns
UPDATE students
SET email = '[email protected]', age = 20
WHERE name = 'Anu';
3. Update all rows
UPDATE students
SET dept_id = 1;
7. Using WHERE Clause
1. Basic WHERE with equality
SELECT * FROM students
WHERE age = 20;
2. Using AND with multiple conditions
SELECT * FROM students
WHERE age > 18 AND dept_id = 1;
3. Using OR for alternate matches
SELECT * FROM students
WHERE name = 'Ram' OR name = 'Anu';
4. WHERE with IN operator
SELECT * FROM students
WHERE dept_id IN (1, 2);
5. WHERE with NOT IN
SELECT * FROM students
WHERE dept_id NOT IN (3);
6. WHERE with BETWEEN for range
SELECT * FROM students
WHERE age BETWEEN 18 AND 22;
7. WHERE with LIKE for pattern match
SELECT * FROM students
WHERE name LIKE 'R%';
8. WHERE with IS NULL
SELECT * FROM students
WHERE email IS NULL;
9. WHERE with != or <> (not equal)
SELECT * FROM students
WHERE dept_id != 2;
10. WHERE with LIMIT and ORDER BY
SELECT * FROM students
WHERE age >= 18
ORDER BY age DESC
LIMIT 3;
8. DELETE, TRUNCATE, DROP
1. Delete a specific record
DELETE FROM students WHERE student_id = 103;
2. Truncate marks table (removes all data, keeps structure)
TRUNCATE TABLE marks;
3. Drop departments table completely
DROP TABLE departments;
9. GROUP BY – Grouping Data
1. Use Case: Count number of students per department
SELECT dept_id, COUNT(*) AS total_students
FROM students
GROUP BY dept_id;
2. Use Case: Average marks per student
SELECT student_id, AVG(score) AS avg_score
FROM marks
GROUP BY student_id;
10. HAVING – Filtering Grouped Data
Use Case: Students with average marks above 80
SELECT student_id, AVG(score) AS avg_score
FROM marks
GROUP BY student_id
HAVING AVG(score) > 80;
HAVING is used after GROUP BY to filter grouped results (unlike WHERE, which filters rows
before grouping).
Use Case: Subjects with average score greater than 80
SELECT subject, AVG(score) AS avg_score
FROM marks
GROUP BY subject
HAVING AVG(score) > 80;
Use Case: Departments with more than 1 student
SELECT dept_id, COUNT(*) AS total_students
FROM students
GROUP BY dept_id
HAVING COUNT(*) > 1;
11. JOIN – Combining Tables
1. INNER JOIN: Combine students and departments to show names with department
names
SELECT s.name, d.dept_name
FROM students s
JOIN departments d ON s.dept_id = d.dept_id;
2. INNER JOIN with marks: Get student names with their marks
SELECT s.name, m.subject, m.score
FROM students s
JOIN marks m ON s.student_id = m.student_id;
3. LEFT JOIN: Show all students, even if they don’t have marks
SELECT s.name, m.subject, m.score
FROM students s
LEFT JOIN marks m ON s.student_id = m.student_id;
4. RIGHT JOIN: Show all marks, even if the student record is missing (less common)
SELECT s.name, m.subject, m.score
FROM students s
RIGHT JOIN marks m ON s.student_id = m.student_id;
5. FULL OUTER JOIN: Combine all records (not supported in MySQL directly, but can be
emulated)
SELECT s.name, m.subject, m.score
FROM students s
LEFT JOIN marks m ON s.student_id = m.student_id
UNION
SELECT s.name, m.subject, m.score
FROM students s
RIGHT JOIN marks m ON s.student_id = m.student_id;
12. UNION vs UNION ALL
1. UNION
SELECT name FROM students
UNION
SELECT name FROM departments;
2. UNION ALL
SELECT name FROM students
UNION ALL
SELECT name FROM departments;
13. VIEW statement
CREATE VIEW student_scores AS
SELECT s.name, m.subject, m.score
FROM students s
JOIN marks m ON s.student_id = m.student_id;
SELECT * FROM student_scores;
14. CASE Statement
SELECT name,
CASE
WHEN score >= 90 THEN 'Excellent'
WHEN score >= 75 THEN 'Good'
ELSE 'Average'
END AS performance
FROM marks;

You might also like