Education logo

Top 40+ SQL Query Interview Questions You Must Practice In 2023

SQL Query Interview Questions

By datacademy aiPublished 3 years ago 3 min read

1. What is SQL and why is it used?

SQL (Structured Query Language) is a programming language used to communicate with and manipulate databases. It is used to create, alter, and query relational databases, which store data in tables consisting of rows and columns.

2. What are the different types of SQL commands?

There are several types of SQL commands, including:

Data Definition Language (DDL) commands: These commands are used to define the database structure or schema. Examples include CREATE, ALTER, and DROP.

Data Manipulation Language (DML) commands: These commands are used to manipulate data within the database. Examples include SELECT, INSERT, UPDATE, and DELETE.

Data Control Language (DCL) commands: These commands are used to control access to the database. Examples include GRANT and REVOKE.

3. How do you select all columns from a table?

To select all columns from a table, use the following syntax:

SELECT * FROM table_name;

4. How do you select a specific column from a table?

To select a specific column from a table, use the following syntax:

SELECT column_name FROM table_name;

5. How do you select distinct values from a column?

To select distinct values from a column, use the DISTINCT keyword in your SELECT statement:

SELECT DISTINCT column_name FROM table_name;

6. How do you count the number of rows in a table?

To count the number of rows in a table, use the COUNT function:

SELECT COUNT(*) FROM table_name;

7. How do you create a new table in a database?

To create a new table in a database, use the CREATE TABLE statement:

CREATE TABLE table_name (

column_1 datatype constraint,

column_2 datatype constraint,

...

);

8. How do you add a column to an existing table?

To add a column to an existing table, use the ALTER TABLE and ADD COLUMN statements:

ALTER TABLE table_name

ADD COLUMN column_name datatype constraint;

9. How do you update data in a table?

To update data in a table, use the UPDATE and SET statements:

UPDATE table_name

SET column_name = new_value

WHERE condition;

10. How do you delete data from a table?

To delete data from a table, use the DELETE FROM statement:

DELETE FROM table_name

WHERE condition;

11. How do you insert data into a table?

To insert data into a table, use the INSERT INTO and VALUES statements:

INSERT INTO table_name (column_1, column_2, ...)

VALUES (value_1, value_2, ...);

12. How do you create a primary key in a table?

To create a primary key in a table, use the PRIMARY KEY constraint when defining the column:

CREATE TABLE table_name (

column_name datatype PRIMARY KEY,

...

);

13. How do you create a foreign key in a table?

In SQL, you can create a foreign key constraint on a table using the ALTER TABLE statement, along with the ADD CONSTRAINT clause. The syntax for creating a foreign key is as follows:eign key

ALTER TABLE child_table

ADD CONSTRAINT constraint_name

FOREIGN KEY (column1, column2, ... column_n)

REFERENCES parent_table (column1, column2, ... column_n);

Here, child_table is the name of the table that will have the foreign key, constraint_name is the name you assign to the foreign key constraint, and column1, column2, ... column_n are the columns in the child table that make up the foreign key. The REFERENCES clause specifies the parent table and the columns in the parent table that the foreign key references.

It’s worth noting that foreign key constraints enforce referential integrity, meaning they ensure that the values in the foreign key columns in the child table correspond to existing values in the referenced columns in the parent table.

14. Write an SQL query to fetch the current date-time from the system.

TO fetch the CURRENT DATE IN SQL Server

SELECT GETDATE();

TO fetch the CURRENT DATE IN MYSQL

SELECT NOW();

TO fetch the CURRENT DATE IN Oracle

SELECT SYSDATE FROM DUAL();

15. Write a SQL query to fetch the PatientName in uppercase and state as lowercase. Also use the ALIAS name for the result-set as PatName and NewState.

TO fetch the CURRENT DATE IN SQL Server

For More Information:datacademy.ai

Follow Us on:

YouTube: https://www.youtube.com/@datacademy-ai

Website: https://www.datacademy.ai/

LinkedIn: https://www.linkedin.com/company/datacademy-cloud/

Instagram: https://www.instagram.com/datacademy.ai/

Twitter: https://mobile.twitter.com/DatacademyAi

Facebook:https://www.facebook.com/people/Datacademyai/100086725062389

book reviewscollegecoursesdegreehow tointerviewteacherstudent

About the Creator

datacademy ai

Datacademy.ai is an e-learning platform that aims to make education accessible to everyone, no matter where they are located. We believe that education is the key to unlocking one's potential and we are dedicated... see more

Reader insights

Be the first to share your insights about this piece.

How does it work?

Add your insights

Comments

There are no comments for this story

Be the first to respond and start the conversation.

Sign in to comment

    Find us on social media

    Miscellaneous links

    • Explore
    • Contact
    • Privacy Policy
    • Terms of Use
    • Support

    © 2026 Creatd, Inc. All Rights Reserved.