Coding Dojo

Coding Dojo Blog logo
SQL Interview Questions

15 SQL Interview Questions And Answers

Tech jobs are the most in-demand jobs of 2022 based on data provided by recruiters and career site experts. It’s not surprising since we’re living in the 21st century where technology is fueling industrialization and modern economy.

Tech jobs are the most in-demand jobs of 2019 based on data provided by recruiters and career site experts. It’s not surprising since we’re living in the 21st century where technology is fueling industrialization and modern economy.

In fact, looking at both U.S. News & World Report’s ranking of the 100 best jobs for 2022 and LinkedIn’s top growing jobs list,  tech jobs are routinely among the most promising and fastest growing.

With many companies relying on big data analytics, the need for SQL developers/database developers is high. But despite the need, companies and organizations are conscientious in assessing and hiring prospective applicants and job candidates.

SQL Interview Questions

Just like the other job interviews, SQL job interviews can be intimidating or overwhelming when you aren’t prepared. Therefore, you need a great deal of courage and preparation to knock it off.

Interview questions are assessment tools used by employers to measure job candidates’ quantitative and qualitative characteristics. As an SQL developer, you’ll be assessed based on your answers in the set of questions given by the HR manager or recruiter. Your answers should always be clear and concise but detailed—just enough to demonstrate how deep your understanding is.  

If you want to secure the job, you need maximum preparation to boost your confidence. You need to show them that you’re the right person for the job.

And to help you ace your interview,  here is the list of popular SQL interview questions and that you might encounter in the future:

1. How do you explain what SQL is to someone without a technical background?

SQL or Structured Query Language is a standardized programming language used to access or manipulate data in a database. It was designed to update, add, delete a row of data and retrieve subsets of information within the database.

In other words, a way to “talk” to databases by using coded commands, called SQL queries.

2. What is RDBMS?

RDBMS or Relational Database Management System is a common type of database in which data is stored in tables. It’s a collection of programs that enables developers to administer and interact with a relational database. A relational database is a set of—formally specified as—tables which data can be accessed in many different ways without having to rearrange the data tables.

Most RDBMS use SQL as the standard Application Programming Interface(API) to manage the database.

3. How do you run a SQL query?

Queries are created by using commands. There are four(4) SQL commands:

  • JOIN – used to join related or connected data stored in one or more tables.
  • SELECT – used to select data.
  • WHERE – used to apply specific criteria to what you have selected.
  • UNION – used to add rows to each other.

After this explanation, you should explain how you run an SQL query step by step, in your own words and in a clear and understandable manner.

4. Enumerate the different type of keys and their uses

SQL keys are sets of one or more columns that uniquely identify a record in a database table. They are used in fetching records or data-rows/column from data tables and connect tables or views in databases. Different types of keys are:

  • Candidate Key – A table identifies only one Primary Key for a single or more column. Candidate keys are potential and qualified keys to be the Primary Key.

  • Primary Key – Also called “Primary Keyword,” is a unique data identifier. It identifies telephone numbers, driver’s license numbers, or vehicle identification numbers(VIN).

  • Alternate Key – Alternate Keys are like a Primary Key that can be a column or set of columns on a table which values are always unique. They can contain null values unless it’s clearly specified with a NOT NULL integrity rule. Currently not selected as a primary key of the table but can also be the Primary Key.

Note: A table is composed of Candidate keys, but only one Candidate key can be a Primary key. Once the Primary key is specified, the rest are called Alternate keys.

  • Composite Key – Also known as a compound key or concatenated key. It is a combination of two or more columns in a table used to identify each table row uniquely. If columns are not combined and are taken individually, uniqueness is not guaranteed.

  • Foreign Key – In two tables’ relationship, a primary key of one table is referred to as a foreign key in another table. A foreign key can have duplicate values and can also keep null values if a column is defined to accept nulls.

  • Unique Key -An alternate key that accepts just one value and doesn’t allow duplicate values in a column.

  • Super Key – it’s a set of columns, where each column is functionally dependent. The Super key may hold additional columns that aren’t strictly required to identify each row uniquely. If you add any other column/attribute to a Primary Key, then it becomes a super key, like EmployeeID + FullName, is a Super Key.

5. What is normalization?

Database normalization is a process of reorganizing and normalizing data in relational databases. It works in accordance with a set called Normal Forms, to reduce the amount of repetitive data and improve data integrity. The types of normalization are:

  • 1NF – or first Normal Form, has a rule that an attribute or a column of a table can only hold or contain a single value. To let it hold more than one value, use the Primary key.

  • 2NF – or second Normal Form is the form where a table can only be taken  if it’s fully compliant with the 1NF rules. In this form, the concept of Composite Key doesn’t allow a table to be in the second normal form because only one column should act as the Primary Key.

  • 3NF – or third Normal Form. A table can only be taken to the third normal form if it is in 2NF. In addition,  there should be no transitive functional dependencies.

  • BCNF – or Boyce-Codd Normal Form, and sometimes called 3.5NF. Usually, most of the scenarios won’t reach this form, unless there are anomalies caused by more than one Candidate Key.

6. What is denormalization?

Denormalization is an optimization technique in a database that adds redundant data to one or more tables. It’s a technique applied after doing normalization to avoid costly joins on the relational database.

7. What joins in SQL?

SQL Joins generate a set containing columns from tables and they are used to combine data records from two or more tables in a database. Here are different types of Joins:

  • Inner Join – It selects records with matching values in two or more tables and creates a new result table.
  • Left [Outer] Join – It selects records from the first left-most table with matching right table records.  If there are no matching records, the result is NULL.
  • Right [Outer] Join – It selects records from the second right-most table that have a match from the left table.
  • Full [Outer] Join – It selects all records matching either left all right table records.
  • Left [Outer] Join without Intersection – It returns all rows from the right-hand table designated in the ON condition which also meets the join condition. Additionally, there should be no rows from the left-hand table that match the join condition.
  • Right [Outer] Join without Intersection – It returns all rows from the right-hand table designated in the ON condition that also meets the join condition. Additionally, there should be no rows from the left-hand table that match the join condition.
  • Full [Outer] Join without Intersection – It returns all rows from both tables designated in the ON condition if the join condition is not met (aside from NULL values).

8. What are the different subsets of SQL?

There are three main subsets of the SQL language, each of which has its own special use:

  • Data Control Language (DCL): Used to control access to data in a database. For example, granting or revoking the rights of specific users performing specific tasks.
  • Data Definition Language (DDL): Used to define structures of data. For example, creating, altering/changing, or dropping tables.
  • Data Manipulation Language (DML): Manipulate or access data in a table. For example, inserting, deleting and updating data. However, selecting is a special statement that belongs to this subset even though it’s a read-only command which won’t manipulate data at all.

    9. Explain SQL indexing and enumerate its type

    man writing on invisible screen

    Indexing is a process which makes the data you requested return faster from the specified table. SQL index is just like the index page of a book; it allows the reader to locate the specific information without scanning the whole book. Without it, the SQL will go through a hassle to scan the whole table for the requested data.

    The three types of indexes are Unique Index, Clustered Index, NonClustered Index. Here are their specific uses:

    • Unique Index makes sure every value in a key index column is unique. It doesn’t allow fields to have duplicate or redundant values. A unique index is automatically applied when the primary key is already defined.
    • Clustered Index sorts and stores data rows in a table or view according to their key values. Only one clustered index is allowed per table because data rows can be stored in only on order.
    • Nonclustered Index has a structure separate from the data rows and contains the nonclustered index key values. Each key value has a pointer to the data row which contains the key value.

    10. What are the differences between DELETE and TRUNCATE?

    DELETE COMMAND TRUNCATE COMMAND
    DELETE command is a DML command. TRUNCATE command is a DDL command.
    It is executed using a row lock, each row in the table is locked for deletion. It is executed using a table lock, and the whole table is locked to remove all records.
    WHERE clause can be used with DELETE to filter and delete specific data. WHERE clause can’t be used with TRUNCATE.
    It is used to remove rows from a table basing on WHERE condition. It removes all rows from a table.
    It maintains the log, so it’s slower than TRUNCATE. It is faster performance wise because it has minimal logging in the transaction log.
    It removes rows one at a time and records an entry in the transaction log for each deleted row. It removes the data by deallocating the data pages that are used to store the table data. It records only the page deallocations in the transaction log.
    It uses more transaction space than TRUNCATE statement. It uses less transaction space that the DELETE command.
    It can be used with indexed views. It cannot be used with indexed views.

    11. What is SQL CLAUSE?

    An SQL clause filters rows from the whole set of records. It is used to limit the result set by giving conditions to the query. Examples of clauses are HAVING and WHERE clauses. Both are performing the same function, but their purpose differs.

    HAVING clause filters both values from a group and records from the result before groupings are made. WHERE clause is used to filter rows and the filtering applies on each row. HAVING clause is used before grouping, and WHERE is used after the grouping.

    12. What is a recursive stored procedure?

    A stored procedure is able to call itself up to the boundary condition; maximum nesting level of 32. A recursive procedure helps programmers to use a corresponding set of code for as many times as they please.

    What are the pros and cons of Stored Procedure?

    Pros of a Stored Procedure

    • It’s faster
    • It’s pre-compiled
    • It reduces network traffic
    • It’s reusable
    • It can handle complex operation
    • It supports nested Stored Procedure
    • Its security is high; codes stay in the Database server

    Cons of a Stored Procedure

    • It’s so hard to code, it needs expert developers
    • It’s hard to debug
    • It’s not database independent. Its code may change based on the database server.

    13. Advantages and Disadvantages of Stored Procedure?

    SQL job interview questions may not be all about the tech stuff. There will be questions that would allow them to know how you would work if you get hired. They would assess your ability on how to reassure end users if there would be some issues.

    Your answer should be reassuring to end users. You must address their frustration in a kind way, accept your mistakes and promise to fix the issues as soon as possible. Stating your actions to fix the problem would also help them to calm down and be assured.

    14. How would you reassure a stakeholder who’s concerned about a SQL database slowdown that’s getting in the way of their work?

    SQL job interview questions may not be all about the tech stuff. There will be questions that would allow them to know how you would work if you get hired. They would assess your ability on how to reassure end users if there would be some issues.

    Your answer should be reassuring to end users. You must address their frustration in a kind way, accept your mistakes and promise to fix the issues as soon as possible. Stating your actions to fix the problem would also help them to calm down and be assured.

    15. Tell me about a challenging SQL project you have worked on.

    This question is also for the interviewer to know what kind of project you have already worked on. Specify the problems you’ve encountered during the project and how you approached it. For example, you’ve encountered a bug that was so hard to find. You can add how you reacted and acted while solving this problem. It would build the interviewer’s confidence to hire you.

    You can also grab this time to present your portfolio and showcase your accomplishments. While doing these, you give them a glimpse on how you would work for them when and if you’re hired.

      How to Learn SQL at Coding Dojo

      In Coding Dojo coding bootcamp, we use MYSQL technology to teach RoR, Python, Java, and .NET. We also have a career service team that helps the graduates and even when they’re still students to prepare for their first job after bootcamp.

      Just remember that all your answers should be clear, straightforward, and with confidence. After all, a job interview is one of the best ways to showcase yourself. We, at Coding Dojo, are cheering for you to pass your interview.