20 Most Common SQL Queries with Examples
SQL Queries with Examples
SQL (Structured Query Language) is a standardized programming language used to manage and manipulate relational databases. It provides a way for users to interact with a database, including querying, updating, inserting, and deleting data. SQL enables users to perform a variety of operations such as creating tables, modifying schema, and enforcing constraints to ensure data integrity.
Table Of Content
- SQL Queries with Examples
- 1. SELECT Statement
- 2. SELECT DISTINCT
- 3. WHERE Clause
- 4. AND, OR, NOT Operators
- 5. ORDER BY Clause
- 6. INSERT INTO Statement
- 7. UPDATE Statement
- 8. DELETE Statement
- 9. JOIN Clause (INNER JOIN)
- 10. LEFT JOIN
- 11. RIGHT JOIN
- 12. FULL OUTER JOIN
- 13. GROUP BY Clause
- 14. HAVING Clause
- 15. LIMIT Clause
- 16. BETWEEN Operator
- 17. IN Operator
- 18. LIKE Operator
- 19. COUNT Function
- 20. MAX and MIN Functions
The language is based on a set of commands, such as SELECT, INSERT, UPDATE, and DELETE, which allow users to perform actions on data stored in tables. SQL queries are the backbone of database management systems (DBMS), enabling users to retrieve information efficiently and perform complex data analysis. With SQL, you can filter, sort, and aggregate data, making it an essential tool for anyone working with relational databases.
- SQL Server Course In Chennai
- SQL Internship In Chennai
- Full Stack Developer Course In Chennai
- Front End Development Course In Chennai
- Networking Course In Chennai
- Internships In Chennai
- Internship For CSE Students In Chennai
- Internship For IT Students In Chennai
- Top 50 Mini Project Ideas For College Students
- 15 Unique Web Development Project Ideas For Beginners
SQL queries can be used in various ways to meet the specific needs of different applications. For example, a SELECT query is used to fetch data from one or more tables, and it can be refined with additional clauses such as WHERE, ORDER BY, and GROUP BY to manipulate the output. SQL queries are often used to join multiple tables, allowing users to combine data based on shared relationships. This relational approach to querying allows for more structured and efficient data retrieval. Furthermore,
SQL includes powerful functions such as COUNT, SUM, and AVG, which allow users to perform calculations on data. As databases grow in size and complexity, SQL remains an essential skill for developers, data analysts, and database administrators, enabling them to query, analyze, and manage large datasets effectively.
1. SELECT Statement
The SELECT statement is used to query a database and retrieve data from one or more tables.
Example:
This query retrieves the first_name and last_name columns from the employees table.
2. SELECT DISTINCT
The DISTINCT keyword is used to return only distinct (different) values in a column.
Example:
This query returns all unique department names from the employees table.
3. WHERE Clause
The WHERE clause is used to filter records based on specified conditions.
Example:
This query selects all columns from the employees table where the salary is greater than 50,000.
4. AND, OR, NOT Operators
These logical operators are used to filter records based on multiple conditions.
Example:
This query returns all employees from the HR department who have a salary greater than 50,000.
5. ORDER BY Clause
The ORDER BY clause is used to sort the result set by one or more columns.
Example:
This query retrieves all employees and sorts them by the last_name in ascending order.
6. INSERT INTO Statement
The INSERT INTO statement is used to insert new records into a table.
Example:
This query inserts a new employee record with the provided values into the employees table.
7. UPDATE Statement
The UPDATE statement is used to modify existing records in a table.
Example:
This query updates the salary of the employee with employee_id 101 to 70,000.
8. DELETE Statement
The DELETE statement is used to remove one or more records from a table.
Example:
This query deletes the record of the employee with employee_id 101 from the employees table.
9. JOIN Clause (INNER JOIN)
The JOIN clause is used to combine rows from two or more tables based on a related column.
Example:
This query retrieves the first_name of employees and their corresponding department_name by joining the employees and departments tables.
10. LEFT JOIN
A LEFT JOIN returns all records from the left table and the matching records from the right table.
Example:
This query returns all employees, including those who may not belong to a department.
11. RIGHT JOIN
A RIGHT JOIN returns all records from the right table and the matching records from the left table.
Example:
This query returns all departments, even those without employees assigned to them.
12. FULL OUTER JOIN
A FULL OUTER JOIN returns all records when there is a match in either the left or right table.
Example:
This query returns all employees and all departments, including those with no matching records in the other table.
13. GROUP BY Clause
The GROUP BY clause is used to group rows that have the same values in specified columns into summary rows.
Example:
This query returns the count of employees in each department.
14. HAVING Clause
The HAVING clause is used to filter records after the GROUP BY clause has been applied.
Example:
This query retrieves departments with more than five employees.
15. LIMIT Clause
The LIMIT clause is used to specify the number of records to return.
Example:
This query returns only the first 10 rows from the employees table.
16. BETWEEN Operator
The BETWEEN operator is used to filter records within a range of values.
Example:
This query selects employees with salaries between 40,000 and 80,000.
17. IN Operator
The IN operator is used to filter records based on a list of values.
Example:
This query retrieves all employees from either the HR or Sales departments.
18. LIKE Operator
The LIKE operator is used to search for a specified pattern in a column.
Example:
This query returns employees whose first names start with the letter “J”.
19. COUNT Function
The COUNT function is used to return the number of rows that match a specified condition.
Example:
This query returns the number of employees in the Marketing department.
20. MAX and MIN Functions
The MAX and MIN functions are used to return the highest and lowest values in a column, respectively.
Example:
This query returns the highest and lowest salaries from the employees table.
In conclusion, SQL queries are the foundation of managing and manipulating data within relational databases. They provide a powerful way to retrieve, update, insert, and delete data, making it an indispensable tool for data management and analysis. Whether you’re working with large datasets, complex data relationships, or performing routine database maintenance, SQL ensures that you can interact with your data efficiently and effectively. Mastering SQL not only helps in understanding the core functionality of relational databases but also opens up opportunities for more advanced data analysis and optimization techniques.
If you’re looking to learn more about SQL or need expert assistance in integrating SQL solutions into your projects, feel free to contact us. Visit our website, Kaashiv Infotech, for more information and support tailored to your needs. Our team is here to help you navigate the world of databases and SQL with professional guidance and expertise!

