Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
7 views5 pages

SQL Questions p1

The document provides a comprehensive overview of SQL concepts, including the differences between DBMS and RDBMS, primary and foreign keys, constraints, and types of SQL commands such as DDL and DML. It also covers advanced topics like normalization, indexing, and the use of aggregate functions, along with practical SQL examples. Additionally, it explains the use of UNION, views, triggers, and methods to manipulate data formats.

Uploaded by

dream.2fly.free
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)
7 views5 pages

SQL Questions p1

The document provides a comprehensive overview of SQL concepts, including the differences between DBMS and RDBMS, primary and foreign keys, constraints, and types of SQL commands such as DDL and DML. It also covers advanced topics like normalization, indexing, and the use of aggregate functions, along with practical SQL examples. Additionally, it explains the use of UNION, views, triggers, and methods to manipulate data formats.

Uploaded by

dream.2fly.free
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/ 5

Basic SQL Questions

Q1. Difference Between DBMS and RDBMS


A:

• DBMS (Database Management System): Manages data as files or unstructured records.


No relation between tables.
• RDBMS (Relational DBMS): Manages data as structured tables with relationships (via
primary/foreign keys). Supports constraints and ACID properties.

Feature DBMS RDBMS


Data Storage Files or records Tables with rows & columns
Relationships Not supported Supported via foreign keys
Constraints Limited Fully supported
Examples XML DB, File system MySQL, PostgreSQL, Oracle

Q2. Primary Key and Foreign Key


A:

• Primary Key: Uniquely identifies each record. Cannot be NULL or duplicate.


• Foreign Key: A field that refers to the primary key in another table, creating a
relationship.

sql
CopyEdit
-- Primary key example
CREATE TABLE Departments (
dept_id INT PRIMARY KEY,
dept_name VARCHAR(100)
);

-- Foreign key example


CREATE TABLE Employees (
emp_id INT PRIMARY KEY,
emp_name VARCHAR(100),
dept_id INT,
FOREIGN KEY (dept_id) REFERENCES Departments(dept_id)
);

Q3. Constraints and Their Types


A: Constraints are rules enforced on data columns to maintain accuracy and integrity. Types:

• NOT NULL – Ensures a column cannot have NULL values


• UNIQUE – Ensures all values in a column are unique
• PRIMARY KEY – Combines NOT NULL + UNIQUE
• FOREIGN KEY – Enforces referential integrity
• CHECK – Ensures values satisfy a condition
• DEFAULT – Assigns a default value if none is provided

Q4. DDL and DML Commands


A:

• DDL (Data Definition Language): Defines database structure


o Commands: CREATE, ALTER, DROP, TRUNCATE
• DML (Data Manipulation Language): Modifies data in tables
o Commands: INSERT, UPDATE, DELETE

Q5. Difference Between DELETE, DROP, and TRUNCATE

Operation Deletes Rows? Deletes Table Structure? Rollback Possible? Filters Allowed?
DELETE Yes (row-wise) No Yes Yes (WHERE)
TRUNCATE Yes (all rows) No No (usually) No
DROP Yes Yes No No

Intermediate SQL Interview Questions

Q6. GROUP BY vs ORDER BY

• GROUP BY: Aggregates data into groups (used with functions like SUM, COUNT)
• ORDER BY: Sorts result rows based on column(s)

sql
CopyEdit
SELECT department, COUNT(*) FROM employees GROUP BY department;
SELECT * FROM employees ORDER BY salary DESC;

Q7. Difference Between WHERE and HAVING

• WHERE: Filters rows before aggregation


• HAVING: Filters groups after aggregation
sql
CopyEdit
-- WHERE filters individual rows
SELECT * FROM employees WHERE salary > 5000;

-- HAVING filters groups


SELECT department, COUNT(*) FROM employees GROUP BY department HAVING
COUNT(*) > 10;

Q8. Aggregate Functions with Example


A: Aggregate functions return a single result from multiple rows. Examples:

• SUM(), COUNT(), AVG(), MIN(), MAX()

sql
CopyEdit
SELECT department, AVG(salary) FROM employees GROUP BY department;

Q9. Indexing and Clustered Indexing

• Index: Speeds up query performance by creating a pointer to data rows.


• Clustered Index: Sorts and stores the data rows physically in that order. Only one
clustered index per table.
• Non-clustered Index: Stores pointers separately from data.

sql
CopyEdit
-- Create a clustered index
CREATE CLUSTERED INDEX idx_salary ON employees(salary);

Q10. Normalization and Its Types


Normalization is the process of organizing data to reduce redundancy.

• 1NF (First Normal Form): Atomic values, no repeating groups


• 2NF: 1NF + no partial dependencies
• 3NF: 2NF + no transitive dependencies
• BCNF: Advanced version of 3NF

Advanced SQL Questions (Experienced)

Q11. UNION vs UNION ALL


• UNION: Combines results of two queries and removes duplicates
• UNION ALL: Combines all rows including duplicates

sql
CopyEdit
SELECT name FROM customers
UNION
SELECT name FROM suppliers;

Q12. Query to Find Second Highest Salary

sql
CopyEdit
SELECT MAX(salary)
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);

Or using ROW_NUMBER():

sql
CopyEdit
SELECT salary
FROM (
SELECT salary, ROW_NUMBER() OVER (ORDER BY salary DESC) AS rnk
FROM employees
) t
WHERE rnk = 2;

Q13. Views in SQL


A: A view is a virtual table based on a SELECT query. It does not store data itself.

sql
CopyEdit
CREATE VIEW high_salary_emps AS
SELECT name, salary FROM employees WHERE salary > 5000;

Q14. How to Convert Text into Date Format?


Use CAST() or TO_DATE() (depends on DB):

sql
CopyEdit
-- PostgreSQL/Oracle
SELECT TO_DATE('2025-06-01', 'YYYY-MM-DD');

-- SQL Server
SELECT CAST('2025-06-01' AS DATE);
Q15. What are Triggers in SQL?
A: Triggers are stored procedures that automatically execute in response to events (e.g.,
INSERT, UPDATE, DELETE) on a table.

sql
CopyEdit
CREATE TRIGGER trg_update_salary
BEFORE UPDATE ON employees
FOR EACH ROW
BEGIN
-- logic here
END;

You might also like