A compilation of 20 questions asked at MAANG (Meta, Apple, Amazon, Netflix and Google).
Blog
A1. DDL stands for Data Definition Language and consists of commands that define the database schema. Some examples are CREATE, ALTER, DROP and TRUNCATE. These commands modify the structure of the database.
DML stands for Data Manipulation Language and consists of commands that manipulate the data itself. Some examples are INSERT, UPDATE and DELETE. These commands modify the data contained within the database.
DCL stands for Data Control Language and consists of commands that deal with rights, permissions and other controls of the database system. Some examples are GRANT and REVOKE. These commands control access to the database.
A2. To get the second highest salary, we first need to rank the salaries in descending order. Then we can limit the result set to the second row, which will contain the second-highest salary.
sqlCopy codeSELECT salary
FROM employee
ORDER BY salary DESC
LIMIT 1 OFFSET 1;
sqlCopy codeSELECT salary
FROM employee
ORDER BY salary DESC
LIMIT 1 OFFSET 1;
The ORDER BY sorts the salaries in descending order.
The LIMIT 1 OFFSET 1 limits the result to 1 row, offset by 1 row, thereby skipping the highest salary and returning the second highest salary.
A3. To retrieve duplicate records from a table, we can use a self-join along with a HAVING clause. The idea is to join the table to itself based on matching column values to identify duplicates.
For example:
sqlCopy codeSELECT t1.*
FROM table t1
INNER JOIN table t2
ON t1.column1 = t2.column1 AND t1.column2 = t2.column2
GROUP BY t1.column1, t1.column2
HAVING COUNT(*) > 1;
This performs a self-join on the same table, matches the rows where the join columns are identical, groups the identical matches and filters for group counts greater than 1 to only return the duplicate rows.
A4. A primary key is a column or set of columns that uniquely identifies each row in the table. It enforces uniqueness and cannot contain NULL values. A table can only have one primary key.
A foreign key is a column or set of columns that establishes a link between data in two tables.
It refers to the primary key in another table, thereby creating a parent-child relationship between the two tables.
Foreign keys ensure data integrity and allow navigation between tables.
A5. Views are virtual tables based on the result set of a SQL query. We can query views like regular tables.
Some key benefits of using views are:
A6. A self-join is when you join a table to itself, essentially treating it as two separate tables. It is used when you need to create a relation between rows in the same table.
Use cases include:
A7. The main types of joins are:
INNER JOIN: Returns rows where there is a match between both tables. It is the most common type of join.
LEFT JOIN: Returns all rows from the left table and matches rows from the right table. Rows with no match in the right table contain NULLs.
RIGHT JOIN: Returns all rows from the right table and matched rows from the left table. Unmatched rows contain NULLs.
FULL OUTER JOIN: Returns all rows from both tables, including unmatched rows with NULLs from either side.
A8. A stored procedure is a precompiled collection of SQL statements stored on the database server. It can accept parameters, perform operations and return data.
Key benefits include:
A9. Clustered indexes physically sort rows in the table based on the index column. Only one clustered index is allowed per table.
Non-clustered indexes contain the index value and row locators pointing to the data. Multiple non-clustered indexes can be created and data is not sorted.
Clustered indexes are faster to read data in order but slower for write operations. Non-clustered indexes avoid sort overhead but are slower to read certain data.
A10. Normalization optimises database structure to reduce data redundancy and anomalies.
1NF - atomic values, no repeating columns or groups
2NF - no partial dependencies on primary keys
3NF - no transitive dependencies on non-primary keys
Higher normal forms continue to drive atomicity and reduce inter-column dependencies.
Finding optimal normalization requires balancing performance vs redundancy.
A11. Some ways to prevent SQL injection attacks:
A12. Aggregate functions perform calculations on multiple values to return a single scalar value.
Examples:
A13. Both combine the result set of two or more SELECT statements.
UNION removes duplicate rows between the individual result sets.
UNION ALL does not remove duplicates, giving a complete combined result set including duplicates.
A14. Window functions perform calculations across rows of a query result set. They allow access to more than just the current row in a query.
Example:
sqlCopy codeSELECT
Salary,
AVG(Salary) OVER() AS AverageSalary
FROM employees;
This calculates the average salary over the entire window partition, for each row.
A15. A CTE or common table expression is a temporary result set defined in the SQL query. It can then be referenced in subsequent parts of the query.
Use cases:
A16. ACID refers to key properties of database transactions:
Atomicity - Ensures the entire transaction succeeds or fails as a whole. If any part fails, the entire transaction is rolled back.
Consistency - The database is in a valid state when the transaction starts and ends. Referential integrity constraints help maintain consistency.
Isolation - Concurrent executions of transactions produce results identical to executing them serially. Isolation levels like READ COMMITTED prevent dirty reads.
Durability - Once committed, a transaction persists even in the event of power loss, crashes or errors. Write-ahead logging helps achieve durability.
ACID properties are ensured through:
This helps maintain data integrity and consistency in databases.
A17. Use ranking window functions like RANK() or DENSE_RANK():
sqlCopy codeSELECT Salary
FROM (
SELECT Salary, RANK() OVER (ORDER BY Salary DESC) rnk
FROM employees
) t
WHERE rnk = N;
This assigns a rank number to each row ordered by salary. Filtering where rank = N gives the Nth salary.
A18. WHERE filters rows before aggregation. HAVING filters rows after aggregation.
WHERE operates on individual rows before GROUP BY. HAVING operates on grouped rows after GROUP BY.
A19. The advantages of prepared statements are as follows:
A20. Techniques to optimise slow queries:
Use these questions to prepare for your upcoming SQL interviews. If you have any questions free free to reach out.
All the best!
Copyright Β©2024 Preplaced.in
Preplaced Education Private Limited
Ibblur Village, Bangalore - 560103
GSTIN- 29AAKCP9555E1ZV