Open In App

10 Most Common SQL Queries That You Should Know

Improve
Improve
Like Article
Like
Save
Share
Report

To handle huge amounts of data, companies rely on SQL which stands for Structured Query Language which is used to communicate with the database. It provides a path through which we can access the database without going through complicated procedures. All the operations you can do follows the CRUD acronym.

Most Common and Important SQL Queries

CRUD stands for the four most important operations that you can perform on a database: Create, Read, Update, and Delete.
In this article, we’ll discuss the Top 10 Most Common SQL Commands and Queries that can help you as a developer when you work with databases while building applications.

What is SQL?

SQL is a language/query used to manage data stored in relational databases. It allows you to create table, modify it, search for the data in the database. SQL contains queries used to modify changes in the existing table or creating a new table. 

10 Most Common SQL Queries

1. Create a Table

CREATE statement in SQL is used to create a new table in your database. 

Syntax to CREATE a table: 

CREATE TABLE table_name(column1 datatype, column2 datatype, … columnN datatype);

Below is the query to create a table employee in your database:

CREATE TABLE Employee (EMP_ID int, NAME varchar (255), SALARY int,  AGE int);

Output:

EMP_ID NAME SALARY AGE

2. Select Query

SELECT is the most commonly used statement in SQL. The SELECT Statement in SQL is used for viewing all records from a table. The data returned is stored in a result table. These result tables are called result sets. 

Syntax to SELECT a query:

SELECT column1, column2, columnN FROM table_name;

where, column1, column2… are the names of fields of a table, table_name is from where we will fetch the records.

Sample Table: Employee (table_name)

EMP_ID NAME SALARY AGE
1 Rajesh 25000 30
2 Suresh 30000 45
3 Andy 28000 40
4 Charlie 40000 36

Below is the query to fetch the fields EMP_ID, NAME, and AGE from the table Employee:

SELECT EMP_ID, NAME, AGE FROM Employee;                                                         

Output:

EMP_ID NAME AGE
1 Rajesh 30
2 Suresh 45
3 Andy 40
4 Charlie 36

Below is the given syntax, if you want to fetch all the fields in the table:

SELECT * FROM table_name;

To fetch all the fields from the table Employee:

SELECT * FROM Employee;

Output:

EMP_ID NAME SALARY AGE
1 Rajesh 25000 30
2 Suresh 30000 45
3 Andy 28000 40
4 Charlie 40000 36

3. Insert Query

The INSERT Statement in SQL is used to insert records into a table. The INSERT INTO declaration is used to insert new records in a table. 

Syntax to INSERT a tuple:

INSERT INTO table_name (column1, column2, … columnN) VALUES (value1, value2, … valueN;

Here, column1, and column2… are the names of fields of a table.

Below is the query to add the fields EMP_ID, NAME, and AGE in the table Employee:

INSERT INTO employee (EMP_ID, NAME, SALARY, AGE) VALUES ('1', 'Rajesh', 25000, 30);      

Output:

EMP_ID NAME SALARY AGE
1 Rajesh 25000 30

Adding more records to the employee table:

INSERT INTO employee (EMP_ID, NAME, SALARY, AGE) VALUES ('2', 'Suresh', 30000, 45);
INSERT INTO employee (EMP_ID, NAME, SALARY, AGE) VALUES ('3', 'Andy', 28000, 40);   

Output:

EMP_ID NAME SALARY AGE
1 Rajesh 25000 30
2 Suresh 30000 45
3 Andy 28000 40

4. Delete Records From a Table

The DELETE Statement in SQL is used to delete some selected records from a table. 

Syntax to DELETE a record:

DELETE FROM employee WHERE [condition];

Below is the query to delete the record from the table Employee:

DELETE FROM employee WHERE EMP_ID = '1';                                                         

Output:

EMP_ID NAME SALARY AGE
2 Suresh 30000 45
3 Andy 28000 40
4 Charlie 40000 36

5. Update Data in Records From a Table

The UPDATE Statement in SQL is used to update/modify the data in the existing records from a table. 

Syntax to UPDATE records:

UPDATE table_name SET column1 = value1, column2 = value2, … WHERE [condition];

Below is the query to delete the record from the table Employee:

UPDATE employee SET age = 38 WHERE EMP_ID = '4';                
SELECT * FROM employee;                                     

Output:

EMP_ID NAME SALARY AGE
2 Suresh 30000 45
3 Andy 28000 40
4 Charlie 40000 38

6. Viewing Only Selected Records From a Table

Using this query, we can get only the selected records from the table.

Syntax to view only the selected records:

SELECT COUNT (1) FROM table_name;

Below is the query to view only the selected records from the table Employee:

SELECT COUNT (1) FROM employee;                                      

Output:

EMP_ID NAME SALARY AGE
2 Suresh 30000 45

7. Viewing Records From a Table

Using this query, we can get the selected records without knowing the exact details from the table.

Syntax to UPDATE records:

SELECT * FROM table_name WHERE [condition];

Below is the query to view the records from the table Employee without knowing the details:

SELECT * FROM employee WHERE name LIKE 'a%y';                

Output:

EMP_ID NAME SALARY AGE
3 Andy 28000 40

8. Where Query

Below is the query to retrieve records using more than one condition using the WHERE statement from the table:

SELECT * FROM employee WHERE name = 'Andy' AND age = 40;                

Output:

EMP_ID NAME SALARY AGE
3 Andy 28000 40

You can also merge AND & OR to the WHERE statement.

9. Viewing Only Selected Columns From a Table

Below is the query to view only the selected columns from the table Employee:

SELECT name FROM employee WHERE age > 39;                

Output:

NAME
Suresh
Andy

You can also select more than one column from the table:

SELECT name, emp_id FROM employee WHERE age > 39;            

Output:    

EMP_ID NAME
2 Suresh
3 Andy

10. Performance of the Query

Below is the advanced query which is useful if you need to get why a query is so slow.

EXPLAIN QUERY PLAN SELECT * FROM EMPLOYEE;                

This query gives you the query cost of all the operations done.

Conclusion

SQL is easy to learn and it enables you to perform many operations with high efficiency and speed. SQL is a massive technology and it is continuously developing new features in every field. A software developer can use Structures Query Language(SQL) for manipulating data, building applications, and also for database-driven tasks. Although the list is not just limited to this, these were the top 10 most common SQL queries for beginners. These SQL queries will help all of the developers to use them while building the applications. 

Related Resources



Last Updated : 14 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads