20 Most Common SQL Queries with Examples

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.

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 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:

<span class="hljs-keyword">SELECT</span> first_name, last_name <span class="hljs-keyword">FROM</span> employees;

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:

<span class="hljs-keyword">SELECT</span> <span class="hljs-keyword">DISTINCT</span> department <span class="hljs-keyword">FROM</span> employees;

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:

<span class="hljs-keyword">SELECT</span> <span class="hljs-operator">*</span> <span class="hljs-keyword">FROM</span> employees <span class="hljs-keyword">WHERE</span> salary <span class="hljs-operator">></span> <span class="hljs-number">50000</span>;

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:

<span class="hljs-keyword">SELECT</span> <span class="hljs-operator">*</span> <span class="hljs-keyword">FROM</span> employees <span class="hljs-keyword">WHERE</span> department <span class="hljs-operator">=</span> <span class="hljs-string">'HR'</span> <span class="hljs-keyword">AND</span> salary <span class="hljs-operator">></span> <span class="hljs-number">50000</span>;

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:

<span class="hljs-keyword">SELECT</span> <span class="hljs-operator">*</span> <span class="hljs-keyword">FROM</span> employees <span class="hljs-keyword">ORDER</span> <span class="hljs-keyword">BY</span> last_name <span class="hljs-keyword">ASC</span>;

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:

<span class="hljs-keyword">INSERT</span> <span class="hljs-keyword">INTO</span> employees (first_name, last_name, department, salary)
<span class="hljs-keyword">VALUES</span> (<span class="hljs-string">'John'</span>, <span class="hljs-string">'Doe'</span>, <span class="hljs-string">'Marketing'</span>, <span class="hljs-number">60000</span>);

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:

<span class="hljs-keyword">UPDATE</span> employees <span class="hljs-keyword">SET</span> salary <span class="hljs-operator">=</span> <span class="hljs-number">70000</span> <span class="hljs-keyword">WHERE</span> employee_id <span class="hljs-operator">=</span> <span class="hljs-number">101</span>;

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:

<span class="hljs-keyword">DELETE</span> <span class="hljs-keyword">FROM</span> employees <span class="hljs-keyword">WHERE</span> employee_id <span class="hljs-operator">=</span> <span class="hljs-number">101</span>;

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:

<span class="hljs-keyword">SELECT</span> employees.first_name, departments.department_name
<span class="hljs-keyword">FROM</span> employees
<span class="hljs-keyword">INNER</span> <span class="hljs-keyword">JOIN</span> departments <span class="hljs-keyword">ON</span> employees.department_id <span class="hljs-operator">=</span> departments.department_id;

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:

<span class="hljs-keyword">SELECT</span> employees.first_name, departments.department_name
<span class="hljs-keyword">FROM</span> employees
<span class="hljs-keyword">LEFT</span> <span class="hljs-keyword">JOIN</span> departments <span class="hljs-keyword">ON</span> employees.department_id <span class="hljs-operator">=</span> departments.department_id;

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:

<span class="hljs-keyword">SELECT</span> employees.first_name, departments.department_name
<span class="hljs-keyword">FROM</span> employees
<span class="hljs-keyword">RIGHT</span> <span class="hljs-keyword">JOIN</span> departments <span class="hljs-keyword">ON</span> employees.department_id <span class="hljs-operator">=</span> departments.department_id;

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:

<span class="hljs-keyword">SELECT</span> employees.first_name, departments.department_name
<span class="hljs-keyword">FROM</span> employees
<span class="hljs-keyword">FULL</span> <span class="hljs-keyword">OUTER</span> <span class="hljs-keyword">JOIN</span> departments <span class="hljs-keyword">ON</span> employees.department_id <span class="hljs-operator">=</span> departments.department_id;

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:

<span class="hljs-keyword">SELECT</span> department, <span class="hljs-built_in">COUNT</span>(<span class="hljs-operator">*</span>)
<span class="hljs-keyword">FROM</span> employees
<span class="hljs-keyword">GROUP</span> <span class="hljs-keyword">BY</span> department;

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:

<span class="hljs-keyword">SELECT</span> department, <span class="hljs-built_in">COUNT</span>(<span class="hljs-operator">*</span>)
<span class="hljs-keyword">FROM</span> employees
<span class="hljs-keyword">GROUP</span> <span class="hljs-keyword">BY</span> department
<span class="hljs-keyword">HAVING</span> <span class="hljs-built_in">COUNT</span>(<span class="hljs-operator">*</span>) <span class="hljs-operator">></span> <span class="hljs-number">5</span>;

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:

<span class="hljs-keyword">SELECT</span> <span class="hljs-operator">*</span> <span class="hljs-keyword">FROM</span> employees LIMIT <span class="hljs-number">10</span>;

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:

<span class="hljs-keyword">SELECT</span> <span class="hljs-operator">*</span> <span class="hljs-keyword">FROM</span> employees <span class="hljs-keyword">WHERE</span> salary <span class="hljs-keyword">BETWEEN</span> <span class="hljs-number">40000</span> <span class="hljs-keyword">AND</span> <span class="hljs-number">80000</span>;

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:

<span class="hljs-keyword">SELECT</span> <span class="hljs-operator">*</span> <span class="hljs-keyword">FROM</span> employees <span class="hljs-keyword">WHERE</span> department <span class="hljs-keyword">IN</span> (<span class="hljs-string">'HR'</span>, <span class="hljs-string">'Sales'</span>);

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:

<span class="hljs-keyword">SELECT</span> <span class="hljs-operator">*</span> <span class="hljs-keyword">FROM</span> employees <span class="hljs-keyword">WHERE</span> first_name <span class="hljs-keyword">LIKE</span> <span class="hljs-string">'J%'</span>;

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:

<span class="hljs-keyword">SELECT</span> <span class="hljs-built_in">COUNT</span>(<span class="hljs-operator">*</span>) <span class="hljs-keyword">FROM</span> employees <span class="hljs-keyword">WHERE</span> department <span class="hljs-operator">=</span> <span class="hljs-string">'Marketing'</span>;

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:

<span class="hljs-keyword">SELECT</span> <span class="hljs-built_in">MAX</span>(salary), <span class="hljs-built_in">MIN</span>(salary) <span class="hljs-keyword">FROM</span> employees;

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!

Previous Article

Top 15 Key Features of Python Every Beginner Should Know in 2025

Next Article

Top 100 Mini Project Ideas For College Students ( July 2025 )

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Subscribe to our Newsletter

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨