SQL
SQL
SQL
1. **`SELECT`**:
```sql
```
2. **`SELECT ALL`**:
```sql
```
3. **`WHERE`**:
```sql
```
4. **`DISTINCT`**:
```sql
```
5. **`ORDER BY`**:
```sql
```
6. **`AND`**:
```sql
SELECT name FROM employee WHERE department = 'HR' AND salary > 40000;
```
7. **`OR`**:
```sql
SELECT name FROM employee WHERE department = 'HR' OR salary > 40000;
```
8. **`NOT`**:
```sql
```
```sql
INSERT INTO employee (name, department, salary) VALUES ('John', 'IT',
50000), ('Jane', 'HR', 60000);
```
```sql
```
11. **`MIN`**:
```sql
```
12. **`IN`**:
```sql
```
13. **`BETWEEN`**:
```sql
SELECT name FROM employee WHERE salary BETWEEN 30000 AND 60000;
```
14. **`GROUP BY`**:
```sql
```
15. **`HAVING`**:
```sql
```
```sql
CREATE TABLE employee (id INT PRIMARY KEY, name VARCHAR(50), salary
INT);
```
```sql
```
18. **`DELETE COLUMN`** (not directly supported in all databases; use drop
column):
```sql
```
```sql
```
- **Starts with**:
```sql
```
- **Ends with**:
```sql
```
- **Contains**:
```sql
SELECT name FROM employee WHERE name LIKE '%an%';
```
#1. Write an SQL query to retrieve the second highest salary from the employee
table.
FROM employee
- Explanation:
- The outer query finds the maximum salary below the highest salary.
FROM employee
LIMIT 1 OFFSET 1;
#### **2. Explain the difference between INNER JOIN and LEFT JOIN.**
- **INNER JOIN**:
- Example:
SELECT *
FROM table1
INNER JOIN table2
ON table1.id = table2.id;
```
- **LEFT JOIN**:
- Returns all rows from the left table and the matched rows from the right
table. If no match exists, NULL values are returned for the right table's columns.
- Example:
```sql
SELECT *
FROM table1
ON table1.id = table2.id;
```
#### **4. What is the difference between the DROP and TRUNCATE
commands?**
- **DROP**:
- Deletes the entire table and its structure from the database.
- Example:
```sql
```
- **TRUNCATE**:
- Deletes all rows from the table but keeps its structure intact.
- Example:
```sql
#### **5. How would you add a new column to an existing table? What if you
want to set a default value?**
```sql
ALTER TABLE table_name
```
```sql
#### **6. How do you insert multiple rows into a table in a single query?**
```sql
#### **8. What is the difference between the IN and EXISTS operators?**
- **IN**:
- Example:
```sql
SELECT *
FROM employee
```
- **EXISTS**:
- Example:
```sql
SELECT *
FROM employee e
WHERE EXISTS (
SELECT 1
FROM department d
);
#### **9. What is the difference between scalar and aggregate functions in
SQL?**
- **Scalar Functions**:
- Usage:
```sql
```
- **Aggregate Functions**:
- Usage:
```sql
```
```sql
#### **11. What is the LIMIT clause used for in SQL queries? Provide an
example.**
- **LIMIT**: Restricts the number of rows returned by a query.
- Example:
```sql
SELECT *
FROM employee
LIMIT 5;
```
#### **12. What is a SELF JOIN and when would you use it?**
- Example:
```sql
```
- **JOIN**:
- Combines rows from two or more tables based on related columns.
- Example:
```sql
FROM employee e
JOIN department d
ON e.department_id = d.id;
```
- **UNION**:
- Combines results of two or more `SELECT` queries into a single result set and
removes duplicates by default.
- Example:
UNION
### **14. What is the use of indexes in SQL, and how do they improve query
performance?**
Indexes are used to speed up the retrieval of data from a database. They allow
the database to quickly locate the rows that satisfy a query condition without
scanning the entire table. Indexes improve performance in queries with
`WHERE`, `JOIN`, and `ORDER BY` clauses by reducing the number of rows the
database engine needs to process.
---
### **15. How would you retrieve the top N rows in SQL?**
To retrieve the top N rows, you can use the `LIMIT` clause (in MySQL or
PostgreSQL) or the `TOP` keyword (in SQL Server). These limit the number of
rows returned by a query. In Oracle, you can use `ROWNUM` or `FETCH FIRST N
ROWS ONLY`.
---
---
---
### **18. What is a foreign key, and how does it maintain referential
integrity?**
A foreign key is a column in one table that refers to the primary key in another
table. It maintains referential integrity by ensuring that a value in the foreign
key column matches an existing value in the referenced primary key, thereby
maintaining valid relationships between tables.
---
### **19. What is the difference between GROUP BY and ORDER BY?**
`GROUP BY` is used to group rows with the same values into summary rows,
often with aggregate functions like `SUM` or `COUNT`.
`ORDER BY` is used to sort the rows of a query result in ascending or descending
order.
---
### **20. What is the difference between HAVING and WHERE clauses in
SQL?**
The `WHERE` clause filters rows before any grouping is applied. It is used for
individual rows.
The `HAVING` clause filters groups after they have been created by the `GROUP
BY` clause. It is often used with aggregate functions.
- **Example**:
```sql
```
---
- **Example**:
```sql
BEGIN
END;
```
---
### **3. What is a Trigger?**
- **Example**:
```sql
```
---
- **Types of Normalization**:
1. **First Normal Form (1NF)**: Ensures that all columns have atomic values
(no repeating groups or arrays).
2. **Second Normal Form (2NF)**: Ensures all non-primary key attributes are
fully functionally dependent on the primary key.
---
---
- Example: A database can have a schema defining tables like `employee` and
`department`.
---
- **Types of Constraints**:
1. **PRIMARY KEY**: Ensures unique and non-null values for the column.
- **Example**:
```sql
department_id INT,
);
```