DEV Community

Shreyas Ghanekar
Shreyas Ghanekar

Posted on

Top 5 SQL questions asked in interviews

In this article , you will encounter top sql questions and their answers asked in interviews. This will help you prepare in for interviews.

Question 1 : What is the difference between DELETE and TRUNCATE ?

Ans :

  1. delete command in sql is used to delete a particular row or a set of rows from the table which meet the conditions specified in the query. Syntax : delete from table_name where condition;

  2. truncate command is used to delete all the rows from a table keeping the table structure intact.

Syntax : truncate table table_name;

Question 2 : What are the different subsets of SQL ?

Ans : The different subsets of SQL are DDL, DML, DCL, TCL . Each one is discused in detail below.

  1. DDL ( Data Definition Language ) : This subset mainly consist of sql commands such as CREATE, ALTER, DROP. CREATE command is used to create a new table in sql. ALTER command is used to alter table structure. DROP command is used to drop tables.

  2. DML ( Data Manipulation Language ) : In sql we use certain commands to update, delete or manipulate the contains of a table. These commands are SELECT , INSERT, UPDATE, DELETE. SELECT command is used to display rows of a table. INSERT command is used to add rows in a table. UPDATE command is used to update the values in rows of a table. DELETE command is used to delete rows of a table.

  3. DCL ( Data Control Language ) : DCL in sql is used to revoke or grant access to a user. Example : REVOKE command is used to revoke access from a user. GRANT command is used to grant certain access to a user.

  4. TCL ( Transaction Control Language ) : It is used to manage transactions within the database. TCL commands include COMMIT, ROLLBACK, and SAVEPOINT. COMMIT is used to commit changes made during a transaction, ROLLBACK is used to undo changes made during a transaction, and SAVEPOINT is used to mark a point within a transaction that can be rolled back to if necessary.

Question 3 : What do you mean by DBMS> What are its different types ?

Ans : A DBMS (Database Management System) is a software or technology used to manage data from a database. It allows users to store, modify and retrieve data in an organized way. It also provides security to the database.

These are the types of DBMS :

Hierarchical DBMS

  • Hierarchical DBMS organizes data in a tree-like structure with parent-child relationships.

  • Each parent can have multiple children, but a child can have only one parent.

  • Data navigation is done through paths.

Network DBMS

  • Network DBMS allows more flexible relationships, where a child can have multiple parents.

  • Uses a network model with sets and records.

  • Data navigation is done through sets.

Relational DBMS (RDBMS)

  • Relational DBMS stores data in tables with rows and columns.

  • Relationships are established using keys.

  • Data access is done using SQL (Structured Query Language).

NoSQL DBMS

  • NoSQL DBMS is designed for handling large volumes of unstructured or semi-structured data.

  • Includes document databases, key-value stores, column-family stores, and graph databases.

  • Data access varies depending on the specific NoSQL type.

Object Oriented DBMS (ODBMS)

  • Object Oriented DBMS stores data as objects, which can have attributes and methods.

  • Supports inheritance, encapsulation, and polymorphism.

  • Data access is done using object query language (OQL).

Question 4 : What do you mean by table and field in SQL ?

Ans : Table: A table is a structured collection of related data organized in rows and columns within a database. Each table represents an entity (e.g., “Students” or “Employees”) and stores data for that specific category. For example, a table named Students might store data like student ID, name, age, and department for each student.

Field (or Column): A field is a single attribute or data type in a table, defining what type of information each entry (row) in the table will hold. In the Students table example, fields could include StudentID, Name, Age, and Department. Each field has a specific data type, like INTEGER, VARCHAR, or DATE, which dictates the kind of data that can be stored in that column.

Question 5: What is foreign key?

Ans : A foreign key in SQL is a column (or a set of columns) in one table that references the primary key of another table. This key is used to create a link between two tables, ensuring referential integrity by making sure that the values in the foreign key column(s) match values in the primary key column(s) of the referenced table.
Example

Let’s say you have two tables: Orders and Customers.

Step 1: Create the Customers table

CREATE TABLE Customers (
    CustomerID INT PRIMARY KEY,
    CustomerName VARCHAR(100),
    ContactNumber VARCHAR(15)
);
Enter fullscreen mode Exit fullscreen mode

Step 2: Create the Orders table with a foreign key referencing Customers

CREATE TABLE Orders (
    OrderID INT PRIMARY KEY,
    OrderDate DATE,
    CustomerID INT,
    FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
);
Enter fullscreen mode Exit fullscreen mode

In this example:

  • CustomerID in the Orders table is the foreign key that references

  • CustomerID in the Customers table.

  • This ensures each order is linked to an existing customer.
    Thanks for reading !!

Top comments (0)