SQL Interview Ques-Ans
SQL Interview Ques-Ans
com/in/vivek-kumar-singh-6a32a3190/
For More: Vivek Kumar Singh
- How can you retrieve records from a table where a specific condition is met?
You can use the WHERE clause to filter records based on specific conditions.
SELECT * FROM table_name WHERE condition;
- Explain the difference between the WHERE and HAVING clauses in SQL.
The WHERE clause filters rows before grouping, while the HAVING clause filters
rows after grouping.
The WHERE clause is used in a SQL query to filter rows from a table based on a
specific condition. It operates on individual rows before the data is grouped or
aggregated. It's commonly used to restrict the result set to rows that meet
certain criteria.
The HAVING clause is used in a SQL query to filter the result set after it has been
grouped by using GROUP BY. It operates on aggregated data, allowing you to
filter groups based on conditions. It's often used to restrict the result set to
aggregated values that meet specific criteria.
https://www.linkedin.com/in/vivek-kumar-singh-6a32a3190/
For More: Vivek Kumar Singh
1. INNER JOIN:
An INNER JOIN returns only the rows that have matching values in both tables.
Example:
Suppose you have two tables, "Customers" and "Orders," and you want to
retrieve customers who have placed orders.
Example:
Retrieve all orders and their corresponding customers, including orders without
customers.
- How do you find records that exist in one table but not in another using SQL?
You can use the LEFT JOIN and IS NULL condition to find records that exist in one
table but not in another.
SELECT Customers.CustomerName
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID
WHERE Orders.CustomerID IS NULL;
4. Aggregation:
- Write a query to calculate the average, sum, and count of a specific column.
You can use the AVG(), SUM(), and COUNT() functions for aggregation.
SELECT AVG(column_name), SUM(column_name), COUNT(column_name) FROM
table_name;
- Write a query to retrieve records from one table based on conditions from
another table.
You can use a subquery to achieve this.
SELECT column_name
FROM table_name
WHERE column_name = (SELECT column_name FROM another_table WHERE
condition);
- What are correlated subqueries and how are they different from regular
subqueries?
Correlated subqueries are subqueries that reference columns from the outer
query. Unlike regular subqueries, they depend on the outer query's results.
7. Data Modification:
- Explain the first three normal forms (1NF, 2NF, 3NF) with examples.
1. First Normal Form (1NF):
First Normal Form (1NF) ensures that the data in a table is organized without any
duplicate columns and that each column holds atomic values (values that cannot
be divided further).
For a table to be in 1NF, each cell must contain a single, indivisible value. There
should be no repeating groups or arrays within a single cell.
Example:
Consider a table with a denormalized structure:
| StudentID | Courses |
|-----------|--------------------------|
|1 | Math, Physics, Chemistry |
|2 | English, Biology |
This table violates 1NF because the "Courses" column contains multiple values
separated by commas. To convert it to 1NF, you'd split the courses into separate
rows:
| StudentID | Course |
|-----------|--------------|
|1 | Math |
|1 | Physics |
|1 | Chemistry |
|2 | English |
|2 | Biology |
https://www.linkedin.com/in/vivek-kumar-singh-6a32a3190/
For More: Vivek Kumar Singh
Example:
Consider a table with student grades:
Table 1: Students
| StudentID |
|-----------|
|1 |
|2 |
Table 2: Courses
| Course | Instructor |
|-----------|------------|
| Math | Mr. A |
| Physics | Mr. B |
https://www.linkedin.com/in/vivek-kumar-singh-6a32a3190/
For More: Vivek Kumar Singh
Table 3: Grades
Example:
Consider a table with employee information:
Table 1: Employees
Table 2: Managers
| DepartmentManager | ManagerPhone |
|-------------------|--------------|
| John | 123-456-7890|
| Mary | 987-654-3210|
The ROW_NUMBER function assigns a unique number to each row in the result
set based on the specified ordering. It starts with 1 and increments by 1 for each
subsequent row. If multiple rows have the same values in the ordering columns,
they receive distinct row numbers.
The RANK function assigns a unique rank to each row based on the specified
ordering. If multiple rows have the same values in the ordering columns, they
receive the same rank, and the next rank is skipped.
https://www.linkedin.com/in/vivek-kumar-singh-6a32a3190/
For More: Vivek Kumar Singh
The DENSE_RANK function is similar to RANK, but it doesn't skip ranks when
multiple rows have the same values in the ordering columns. Instead, it assigns
the same rank to those rows and then continues with the next rank.
So, ROW_NUMBER assigns a unique number to each row. RANK gives the same
rank to equal values but leaves gaps. DENSE_RANK gives the same rank to equal
values without gaps.
Good Luck!
Vivek Kumar Singh