st1
st1
Theory Questions
1. What is SQL?
o Describe its purpose and the types of operations it supports.
Ans- SQL (Structured Query Language) is a standard programming
language designed to manage and manipulate relational databases.
Purpose:
o Retrieve, insert, update, and delete data in a database.
o Define database structures (DDL).
o Control database access and security.
Operations:
o Data Querying: SELECT.
o Data Manipulation: INSERT, UPDATE, DELETE.
o Data Definition: CREATE, ALTER, DROP.
o Data Control: GRANT, REVOKE.
Includes only rows with matching keys in both Includes all rows from the left table,
Matching Rows
tables. regardless of a match in the right table.
Foreign Key
Definition: A column or a set of columns in a table that establishes
a relationship between the data in two tables. It references the
primary key in another table.
Key Characteristics:
o Can have duplicate values.
o Can contain NULL values (if the relationship is optional).
o Used to enforce referential integrity.
Purpose: Links two tables and maintains data consistency between
them.
4. What is normalization?
o Explain its importance and the different normal forms.
Ans- Normalization: Organizing database tables to reduce redundancy
and improve data integrity.
Importance:
o Minimizes data anomalies.
o Improves storage efficiency.
Normal Forms:
o 1NF: Atomic values, unique rows.
o 2NF: Satisfies 1NF, no partial dependency.
o 3NF: Satisfies 2NF, no transitive dependency.
o BCNF: Stronger form of 3NF.
Types of Subqueries
1. Single-row Subquery: Returns exactly one value.
Example: Find the highest salary in a specific department.
2. Multi-row Subquery: Returns multiple rows.
Example: List employees working in multiple departments.
3. Correlated Subquery: Executes once for each row in the outer
query.
Example: Compare each employee’s salary to the average salary of
their department.
4. Nested Subquery: A subquery within another subquery.
Example: Find offices based on conditions derived from nested
conditions.
Groups rows with the same Sorts the result set in a specific
Purpose
values into summary rows. order.
SELECT department,
SELECT name, salary FROM
SUM(salary) FROM
Example Employees GROUP BY
Employees ORDER BY salary
DESC;
department;
GROUP BY:
Groups rows with the same values in specified columns into one
group.
Often used with aggregate functions like SUM, AVG, MAX, COUNT.
Example Use Case: Calculate total sales per region.
ORDER BY:
Arranges rows in a specific order, either ascending (ASC) or
descending (DESC).
Can be used independently or after GROUP BY.
Example Use Case: Display a list of employees sorted by their
salary.
Purpose of an Index:
SQL Test for Freshers
Types of Indexes:
1. Single-column Index: Based on a single column.
2. Composite Index: Based on multiple columns.
3. Unique Index: Ensures all values in the indexed column are unique.
4. Full-text Index: Optimized for text searches.
5. Clustered Index: Determines the physical order of data in the
table (one per table).
6. Non-clustered Index: Creates a separate structure pointing to the
table data (can have multiple per table).
Purpose:
Filters the results of grouped data based on conditions applied to
aggregate functions.
Ensures only groups that meet the specified criteria appear in the
result set.
Property Description
SQL Test for Freshers
The database must always be in a valid state before and after the transaction.
Consistency It ensures that the transaction takes the database from one consistent state to
another.
Once a transaction is committed, its changes are permanent and survive any
Durability system crashes. The changes are written to persistent storage (e.g., a hard disk
or database).
Practical Questions
1. Write an SQL query to find the second highest salary from
an employees table
Ans-SELECT MAX(Salary) AS SecondHighestSalary
FROM Employees
WHERE Salary < (SELECT MAX(Salary) FROM Employees);
2. Create a query to count the number of employees in each
department from a departments table.
Ans- SELECT department_id, COUNT(employee_id) AS num_employees
FROM employees
GROUP BY department_id;
3. Write an SQL statement to retrieve all columns from
the products table where the price is greater than 100.
Ans- SELECT * FROM products WHERE price > 100;
4. Write a query to update the email of an employee in
the employees table where the employee ID is 5.
Ans- UPDATE Employees
SET Email = '[email protected]'
WHERE EmployeeID = 5;
5. Create a SQL query to delete all records from
a temp_users table that were created more than 30 days
ago.
Ans- DELETE FROM temp_users
WHERE created_at < CURRENT_DATE - INTERVAL 30 DAY;
Aptitude Questions
1. If a database has 2000 records and you want to find a
specific record with a binary search, how many comparisons
would it take in the worst case?
Ans-
SQL Test for Freshers
4. If you have 5 tables and you need to join all of them, how
many total joins will be required?
Ans-
To calculate the total number of joins required to join 5 tables, you can
use the following formula:
Total Joins=Number of Tables−1\text{Total Joins} = \text{Number of
Tables} - 1Total Joins=Number of Tables−1
This is because, to join nnn tables, you need to perform n−1n - 1n−1
joins.
For 5 Tables:
Total Joins=5−1=4\text{Total Joins} = 5 - 1 = 4Total Joins=5−1=4
You will need 4 joins to join 5 tables.