All topics

SQL interview questions

160 questions, 158 with answers. 4,697 open jobs in India ask for SQL.

  1. How can you deal with duplicate data points in an SQL query?

    To deal with duplicate data points in an SQL query, you can use the DISTINCT keyword in the SELECT statement. The DISTINCT keyword filters out duplicate values from the result set, returning only unique values. Alternatively, you can use the GROUP BY clause in combination with an aggregate function such as COUNT() or SUM() to group the duplicate values together and perform calculations on them.

    From Tech Vault (Apache 2.0)

  2. List objects that are created via the CREATE statement in SQL

    Objects that can be created using the CREATE statement in SQL include tables, views, indexes, sequences, procedures, functions, and triggers.

    From Tech Vault (Apache 2.0)

  3. How would you see the database structure in SQL?

    In SQL, you can use the DESCRIBE statement to see the structure of a specific table, which shows the names, data types, and constraints of the columns in the table. Additionally, you can use the SHOW TABLES statement to see a list of all tables in the current database.

    From Tech Vault (Apache 2.0)

  4. How would you search for a specific string in a column?

    To search for a specific string in a column, you can use the LIKE operator in a SELECT statement. The LIKE operator is used in a WHERE clause to search for a specified pattern in a column. For example, the following SQL query selects all rows from the "customers" table where the "contact_name" column contains the string "John":

    SELECT *

    FROM customers

    WHERE contact_name LIKE '%John%';

    From Tech Vault (Apache 2.0)

  5. What are the differences between DDL, DML and DCL?

    DDL (Data Definition Language) is used to define the database schema and includes commands such as CREATE, ALTER, and DROP. DML (Data Manipulation Language) is used to manipulate the data in the database and includes commands such as SELECT, INSERT, UPDATE, and DELETE. DCL (Data Control Language) is used to manage user access permissions and includes commands such as GRANT and REVOKE.

    From Tech Vault (Apache 2.0)

  6. Difference between SQL and MySQL?

    SQL (Structured Query Language) is a programming language used to manage relational databases. MySQL is a popular open-source relational database management system that uses SQL as its standard programming language.

    From Tech Vault (Apache 2.0)

  7. How is a RDBMS different to a DBMS?

    An RDBMS (Relational Database Management System) is a type of DBMS (Database Management System) that organizes data into one or more tables with a predefined structure, and uses relationships between tables to store and retrieve data. A DBMS, on the other hand, can be any system that manages data, including hierarchical, network, and object-oriented databases.

    From Tech Vault (Apache 2.0)

  8. What is a self join? name other join commands?

    A self join is a join where a table is joined with itself. It is useful when a table contains a foreign key that references its own primary key. Other join commands include INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN.

    From Tech Vault (Apache 2.0)

  9. What is the SELECT statement?

    The SELECT statement is used to retrieve data from one or more tables in a database. It is the most commonly used SQL statement and is used to retrieve specific columns and/or rows that match a specified condition.

    From Tech Vault (Apache 2.0)

  10. What are the CRUD commands?

    CRUD (Create, Read, Update, Delete) commands are used to manipulate data in a database. CREATE is used to create new records, READ is used to retrieve records, UPDATE is used to modify existing records, and DELETE is used to delete records.

    From Tech Vault (Apache 2.0)

  11. What are UNION, MINUS and INTERSECT commands?

    UNION, MINUS, and INTERSECT are set operators that allow combining, subtracting, and intersecting results from two or more SELECT statements in SQL.

    UNION: returns the combined results of two or more SELECT statements, removing duplicates. The number of columns and their data types must be the same in all SELECT statements.

    MINUS (also known as EXCEPT in some versions of SQL): returns the rows from the first SELECT statement that are not present in the second SELECT statement.

    INTERSECT: returns only the rows that are common to both SELECT statements.

    These set operators can be useful in situations where you need to combine, subtract or compare data from multiple tables or queries.

    From Tech Vault (Apache 2.0)

  12. How would you load data into tables using SQL?

    Data can be loaded into tables using SQL by using the "INSERT" statement. This statement is used to add one or more records to a table. The syntax for inserting data into a table is as follows:

    INSERT INTO table_name (column1, column2, column3, ...)

    VALUES (value1, value2, value3, ...);

    In this syntax, "table_name" is the name of the table where data needs to be inserted, and "column1, column2, column3, ..." are the names of the columns where data needs to be inserted. "value1, value2, value3, ..." are the values that need to be inserted in the respective columns.

    From Tech Vault (Apache 2.0)

  13. What is PostgreSQL?

    PostgreSQL is a powerful open-source relational database management system (RDBMS) that supports a wide range of features including transactions, sub-selects, triggers, views, and foreign keys. It is designed to be highly scalable, and can handle large amounts of data and concurrent users. PostgreSQL is known for its stability, reliability, and data integrity.

    From Tech Vault (Apache 2.0)

  14. Explain the character manipulation functions in SQL

    Character manipulation functions in SQL are used to modify strings in a database. These functions include:

    CONCAT: Used to concatenate two or more strings.

    UPPER and LOWER: Used to convert a string to uppercase or lowercase respectively.

    SUBSTRING: Used to extract a substring from a given string.

    LENGTH: Used to get the length of a string.

    TRIM: Used to remove leading or trailing spaces from a string.

    REPLACE: Used to replace a substring within a string with a new substring.

    From Tech Vault (Apache 2.0)

  15. What is the difference between RANK and DENSE_RANK() functions?

    Character manipulation functions in SQL are used to modify strings in a database. These functions include:

    The RANK() and DENSE_RANK() functions are used to assign a rank to each row in a result set based on the values in one or more columns. The main difference between the two functions is how they handle ties.

    The RANK() function assigns the same rank to tied rows, and leaves gaps in the ranking sequence. For example, if two rows have the same value and are assigned rank 2, the next row will be assigned rank 4.

    The DENSE_RANK() function, on the other hand, assigns the same rank to tied rows, but does not leave gaps in the ranking sequence. For example, if two rows have the same value and are assigned rank 2, the next row will be assigned rank 3.

    From Tech Vault (Apache 2.0)

  16. What are tables and fields?r?

    Tables are the fundamental objects in a database, where data is stored in rows and columns. A table is made up of one or more columns, which define the type of data that can be stored in each row of the table. Each column has a name and a data type.

    Fields, also known as columns, are the individual pieces of data stored in a table. Each field has a name, a data type, and a value.

    From Tech Vault (Apache 2.0)

  17. What is a schema in a SQL server?

    A schema in a SQL server is a logical container for database objects, such as tables, views, indexes, and stored procedures. It is a way to organize database objects into groups and assign permissions to those groups. A schema can be used to separate database objects into logical categories, and can be used to isolate different applications or users from each other.

    From Tech Vault (Apache 2.0)

  18. How would you create a table with 4 columns?

    To create a table with 4 columns, you would use the "CREATE TABLE" statement, with the column names and data types specified as follows:

    CREATE TABLE my_table (

    column_1 datatype,

    column_2 datatype,

    column_3 datatype,

    column_4 datatype

    );

    From Tech Vault (Apache 2.0)

  19. What is a CASE statment?

    A CASE statement is a control flow structure in SQL that allows you to add conditional logic to a query. It evaluates a set of conditions and returns a value based on the first true condition. The basic syntax of a CASE statement is:

    CASE

    WHEN condition_1 THEN result_1

    WHEN condition_2 THEN result_2

    ...

    ELSE result_n

    END

    From Tech Vault (Apache 2.0)

  20. Summarise differences between SQL and NoSQL

    SQL (Structured Query Language) and NoSQL (Not Only SQL) are two types of database management systems that differ in their data model, storage architecture, and query language.

    SQL databases are relational databases that store data in tables with fixed schemas. They use SQL as the query language to perform operations like CRUD (Create, Read, Update, Delete) on the data. SQL databases are known for their ability to handle complex queries and transactions, and for their strong consistency and ACID (Atomicity, Consistency, Isolation, Durability) properties. Examples of SQL databases include MySQL, Oracle, and PostgreSQL.

    On the other hand, NoSQL databases are non-relational databases that store data in flexible schemas, such as key-value pairs, documents, or graphs. They typically use a query language other than SQL, such as JSON or MongoDB's query language, to retrieve and manipulate data. NoSQL databases are known for their scalability, high availability, and ability to handle unstructured and semi-structured data. However, they may sacrifice consistency and durability for performance and scalability. Examples of NoSQL databases include MongoDB, Cassandra, and Couchbase.

    From Tech Vault (Apache 2.0)

  21. SQL was developed as an integral part of...

    Relational Database Management Systems (RDBMS). SQL is the ANSI-standard language for creating, querying, updating, and managing relational databases. Major implementations include MySQL, PostgreSQL, SQL Server, Oracle, and SQLite.

    Real-world context: Every web application you use — Instagram, Netflix, Uber — stores its data in SQL databases (often sharded or replicated, but SQL at the core).

    From Cracking the SQL Interview (MIT)

  22. What does SQL stand for?

    Structured Query Language. Originally called SEQUEL at IBM in the 1970s.

    From Cracking the SQL Interview (MIT)

  23. What type of language is SQL?

    Declarative (non-procedural). You state what result you want, not how to compute it. The database's query optimizer decides the "how" — which indexes to use, which join order is fastest, etc.

    Compare with PL/SQL (Oracle) or T-SQL (SQL Server), which are procedural extensions that add loops, variables, and conditional logic.

    From Cracking the SQL Interview (MIT)

  24. Which is NOT a valid SQL command: SELECT, REMOVE, UPDATE, INSERT?

    REMOVE is not valid. To delete rows, use DELETE. DROP removes objects (tables, databases).

    Command Purpose
    SELECT Read data
    INSERT Add new rows
    UPDATE Modify existing rows
    DELETE Remove rows
    CREATE/ALTER/DROP Manage schema objects

    From Cracking the SQL Interview (MIT)

  25. What is MySQL, and how does it differ from SQL?

    SQL is the language (like English). MySQL is a database system that speaks SQL (like a person who speaks English).

    Analogy: SQL is the recipe; MySQL is the kitchen that follows it.

    From Cracking the SQL Interview (MIT)

  26. What is PL/SQL, and how is it different from SQL?

    PL/SQL = Procedural Language extensions to SQL (Oracle-specific). It adds variables, loops, IF-ELSE, and exception handling.

    SQL PL/SQL
    Single statements Blocks of code
    No variables Variables & constants
    No loops FOR, WHILE loops
    Data-oriented Application-oriented
    Can embed in PL/SQL Cannot embed SQL inside itself

    From Cracking the SQL Interview (MIT)

  27. What are the possible values for a BOOLEAN field in MySQL?

    MySQL doesn't have a native BOOLEAN type. It uses TINYINT(1) where 0 = false, 1 = true, and any non-zero value is treated as true in conditions.

    CREATE TABLE users (is_active TINYINT(1) DEFAULT 1);
    

    PostgreSQL, however, has a true BOOLEAN type accepting TRUE, FALSE, and NULL.

    From Cracking the SQL Interview (MIT)

  28. Which data type for distance rounded to the nearest mile?

    INTEGER (or INT). If you need fractional miles later, use DECIMAL(8,2).

    From Cracking the SQL Interview (MIT)

  29. Which are valid SQL keywords?

    SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY, JOIN, INSERT, UPDATE, DELETE, CREATE, ALTER, DROP, INDEX, UNION, LIMIT, DISTINCT, AS, AND, OR, NOT, IN, BETWEEN, LIKE, IS NULL, EXISTS, CASE, WHEN, THEN, ELSE, END.

    From Cracking the SQL Interview (MIT)

  30. Which are valid SQL comments?
    -- Single-line comment (standard)
    /* Multi-line
       comment */
    # Hash comment (MySQL only)
    

    From Cracking the SQL Interview (MIT)

After the last page: 2 questions more, without answers. Go to the last page

Related topics

Where these questions come from

Open-source collections on GitHub, official documentation, a government careers service and the Stack Exchange sites, each used under its own licence. The answers are the authors' own words: we only tidy the layout, leave out pictures, and cut a long answer with a link to the rest. Stack Exchange answers stay under CC BY-SA and name who asked and who answered.