Here are five common SQL interview questions that are often asked of freshers:
- What is SQL?
- SQL stands for Structured Query Language. It is a language used to interact with databases. With SQL, you can create databases, tables, retrieve data, update records, and more. It’s an ANSI standard language that communicates with various database management systems like MySQL, Oracle, and PostgreSQL.
- What is a database?
- A database is a structured form of data storage on a computer. It consists of schemas, tables, queries, views, and more. Databases allow us to easily store, access, and manipulate data.
- Does SQL support programming language features?
- SQL is not a programming language; it is a command language. Unlike programming languages, SQL lacks conditional statements like loops or if-else constructs. Instead, it provides commands for querying, updating, and deleting data in the database.
- Difference between CHAR and VARCHAR2 datatype in SQL:
- CHAR is used for fixed-length character strings, while VARCHAR2 is used for variable-length character strings. For example, if you define a column as CHAR(5), it will always store strings of exactly 5 characters. In contrast, VARCHAR2(5) allows variable-length strings.
- What is data definition language (DDL)?
- DDL includes commands like CREATE, DROP, and ALTER. These queries define the structure of data, such as creating tables or modifying their properties.
- What is a JOIN in SQL?
- A JOIN combines data from two or more tables based on a common column. It allows you to use data from multiple tables simultaneously. There are different types of JOINs, such as INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL JOIN.
- Example: To retrieve data from the
football_players
andnational_teams
tables, you can use an INNER JOIN on thenational_team_id
column.
- What is a subquery in SQL?
- A subquery (also known as a nested query) is a query embedded within another query. It allows you to retrieve data from one table based on the results of another query. Subqueries are often used in WHERE or HAVING clauses.
- Example: To find the average GPA of students who play more than 30 games, you can use a subquery.
- What are window functions in SQL?
- Window functions perform calculations across a set of rows related to the current row. They allow you to create running totals, rankings, and moving averages. Common window functions include RANK(), DENSE_RANK(), and SUM() OVER().
- Example: To compute the cumulative GPA for each student, you can use a window function.
- What’s the difference between ROWS and RANGE in window functions?
- In window functions, ROWS and RANGE define the window frame. ROWS specifies a fixed number of rows, while RANGE defines a range of values based on the order of rows. The choice between them depends on the specific use case.
- Example: To calculate a moving average, you might use RANGE BETWEEN 3 PRECEDING AND 3 FOLLOWING.
- How do you create a ranking in SQL?
- You can create rankings using the RANK() or DENSE_RANK() window functions. RANK() assigns the same rank to equal values, while DENSE_RANK() leaves no gaps in ranking. Both functions order rows based on specified columns.
- Example: To rank students by GPA, you can use the RANK() function.