SQL
30 Questions
Practice Problems
SQL Interview Questions
30 Questions
SQL Basics
What is SQL and why is it used in Data Science?
SQL stands for Structured Query Language and is used to manage relational databases. In Data Science, SQL helps extract, clean, transform, and analyze large datasets efficiently. It is widely used for querying business data before performing visualization, reporting, or machine learning tasks.
What is the difference between WHERE and HAVING in SQL?
WHERE filters rows before applying aggregation functions, while HAVING filters grouped records after aggregation. WHERE cannot directly use aggregate functions like COUNT() or SUM(), but HAVING can. Both clauses help filter data, but they are used at different stages of query execution.
What is the difference between DELETE, DROP, and TRUNCATE?
DELETE removes selected rows and can be rolled back. TRUNCATE removes all rows quickly and resets identity values, but usually cannot be rolled back. DROP permanently deletes the entire table structure along with all its data, indexes, and relationships from the database.
What are primary keys and foreign keys?
A primary key uniquely identifies every row in a table and does not allow NULL values. A foreign key creates a relationship between two tables by referencing the primary key of another table. These keys help maintain data consistency and relational integrity in databases.
What is normalization in SQL?
Normalization is the process of organizing database tables to reduce data redundancy and improve consistency. It divides large tables into smaller related tables using relationships. Common normalization levels include First Normal Form, Second Normal Form, and Third Normal Form for efficient database design.
What is denormalization?
Denormalization combines multiple related tables into fewer tables to improve query performance and reduce complex joins. Although it increases data redundancy, it helps retrieve data faster. Denormalization is commonly used in reporting systems, analytics databases, and data warehouses for better performance.
What is the difference between CHAR and VARCHAR?
CHAR stores fixed-length strings and always reserves the specified amount of memory, even if the text is shorter. VARCHAR stores variable-length strings and uses memory based on actual data length. VARCHAR is more storage efficient when text lengths vary significantly between records.
What are constraints in SQL?
Constraints are rules applied to database columns to ensure accurate and reliable data storage. Common constraints include PRIMARY KEY, FOREIGN KEY, UNIQUE, NOT NULL, and CHECK. They help maintain data integrity, prevent invalid entries, and enforce relationships between different database tables effectively.
What is NULL in SQL?
NULL represents missing, undefined, or unknown data in SQL databases. It is different from zero or an empty string. Special conditions like IS NULL and IS NOT NULL are used to check NULL values because standard comparison operators do not work correctly with NULL data.
What is the difference between UNION and UNION ALL?
UNION combines results from multiple queries and removes duplicate rows automatically. UNION ALL also combines results but keeps duplicate records, making it faster because no duplicate checking is performed. UNION is preferred when unique results are required in the final dataset.
What are joins in SQL?
Joins are used to combine rows from multiple tables based on related columns. They help retrieve meaningful information stored across different tables. Common join types include INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN, which are widely used in Data Science projects.
What is an INNER JOIN?
INNER JOIN returns only the matching rows from both tables based on a common condition. Rows without matching values are excluded from the final result. It is one of the most commonly used joins for combining related data stored across multiple relational database tables.
What is a LEFT JOIN?
LEFT JOIN returns all rows from the left table and matching rows from the right table. If no matching record exists in the right table, NULL values are returned. It is useful for identifying missing data and keeping complete records from the main table.
What is the difference between LEFT JOIN and INNER JOIN?
INNER JOIN returns only matching records from both tables, excluding unmatched rows. LEFT JOIN returns all rows from the left table even when matching rows are unavailable in the right table. LEFT JOIN is commonly used when preserving all records from one table is necessary.
What is a SELF JOIN?
A SELF JOIN joins a table with itself using aliases. It is mainly used when comparing rows within the same table, such as employee-manager relationships or hierarchical structures. SELF JOIN helps analyze relationships between records stored in a single database table efficiently.
What is a CROSS JOIN?
CROSS JOIN returns the Cartesian product of two tables by combining every row from the first table with every row from the second table. It can generate very large datasets and is mainly used when all possible combinations between two tables are required.
What is a FULL OUTER JOIN?
FULL OUTER JOIN returns all matching and unmatched rows from both tables. If there is no match, NULL values are filled for missing columns. It combines the behavior of LEFT JOIN and RIGHT JOIN and is useful for complete data comparison tasks.
How do you find unmatched records between two tables?
Unmatched records can be identified using LEFT JOIN or FULL OUTER JOIN with NULL checks. Usually, records from one table are joined with another, and rows where matching values are NULL indicate unmatched records. This technique helps identify missing or inconsistent data easily.
Why are joins important in Data Science?
Joins help combine data stored across multiple tables into a single meaningful dataset. Data Scientists use joins to merge customer data, sales information, transactions, and logs for analysis, reporting, and machine learning. Efficient joins are essential for building accurate datasets and insights.
What is the difference between ON and WHERE in joins?
ON defines the condition used to match rows between tables during the join process. WHERE filters the final result after the join operation is completed. Using conditions correctly in ON and WHERE clauses affects both query accuracy and performance significantly in SQL queries.
What are aggregate functions in SQL?
Aggregate functions perform calculations on multiple rows and return a single summarized result. Common aggregate functions include COUNT(), SUM(), AVG(), MIN(), and MAX(). These functions are widely used in reporting, analytics, and Data Science for summarizing and analyzing large datasets efficiently.
What is GROUP BY in SQL?
GROUP BY groups rows with similar values into summary groups for aggregation. It is commonly used with aggregate functions like COUNT(), SUM(), and AVG(). GROUP BY helps analyze data category-wise, such as department-wise salaries, monthly sales reports, or customer transaction summaries.
What is the difference between COUNT(*) and COUNT(column_name)?
COUNT(*) counts all rows in a table, including rows containing NULL values. COUNT(column_name) counts only non-NULL values from the specified column. This difference becomes important while analyzing missing data or calculating accurate counts for particular database columns in reports.
What is the purpose of HAVING clause?
HAVING filters grouped records after applying GROUP BY and aggregate functions. Unlike WHERE, HAVING works with aggregated values like COUNT() and AVG(). It is commonly used to filter groups based on conditions, such as departments having more than ten employees or high sales totals.
How do you find duplicate records in SQL?
Duplicate records are identified using GROUP BY with aggregate functions like COUNT(). Usually, records are grouped based on specific columns, and groups with COUNT greater than one are considered duplicates. This method helps clean datasets and improve data quality before analysis or reporting.
What is DISTINCT in SQL?
DISTINCT removes duplicate values from query results and returns only unique records. It is commonly used when analyzing categories, customer IDs, or repeated values in datasets. DISTINCT improves result clarity and helps generate accurate summaries for reporting and analytical tasks in databases.
What is the difference between WHERE and GROUP BY?
WHERE filters individual rows before grouping occurs, while GROUP BY groups rows based on common column values for aggregation. WHERE works before aggregate calculations, whereas GROUP BY organizes data into summarized groups for analysis using functions like SUM(), COUNT(), or AVG().
How do you calculate average salary department-wise?
Department-wise average salary is calculated using GROUP BY with the AVG() function. The query groups employees by department and calculates average salary values for each group. This method is commonly used in business analytics, HR reporting, and salary analysis tasks within organizations.
What is ORDER BY in SQL?
ORDER BY sorts query results in ascending or descending order based on specified columns. ASC is used for ascending order, while DESC is used for descending order. Sorting improves readability and helps analysts identify top-performing, highest-selling, or latest records efficiently in datasets.
What is LIMIT in SQL?
LIMIT restricts the number of rows returned by a query result. It is commonly used for pagination, retrieving top records, and improving performance when working with large datasets. LIMIT helps analysts quickly preview sample data without loading complete tables into the query output.