Tech

50 SQL Queries Examples

Structured Query Language (SQL) is a powerful tool used for managing and manipulating relational databases. Whether you’re a database administrator, data analyst, or software developer, having a strong grasp of SQL is essential in dealing with data efficiently and effectively.



SQL Queries Examples

In this article, we will explore 50 SQL queries examples grouped into different categories to help you better understand how to use SQL to extract, update, and manipulate data.

If you’re preparing for an interview, you can also check out our 25 sql queries interview questions.

Let’s dive right in

Retrieving Data

Basic SELECT

   – SELECT * FROM employees;

   – SELECT first_name, last_name FROM employees;



Aliases

   – SELECT first_name AS “First Name”, last_name AS “Last Name” FROM employees;

DISTINCT

   – SELECT DISTINCT department FROM employees;

WHERE Clause

   – SELECT * FROM employees WHERE department = ‘Sales’;



   – SELECT * FROM employees WHERE salary > 50000 AND department = ‘IT’;

ORDER BY

   – SELECT * FROM employees ORDER BY hire_date DESC;

   – SELECT * FROM products ORDER BY price ASC;

Aggregating Data

COUNT

   – SELECT COUNT(*) FROM employees;

   – SELECT COUNT(DISTINCT department) FROM employees;

SUM

   – SELECT SUM(salary) FROM employees;

AVG

   – SELECT AVG(salary) FROM employees;

MAX and MIN

   – SELECT MAX(salary) FROM employees;

   – SELECT MIN(salary) FROM employees;

Joining Tables

INNER JOIN

   – SELECT employees.first_name, departments.department_name

     FROM employees

     INNER JOIN departments ON employees.department_id = departments.department_id;

LEFT JOIN

   – SELECT employees.first_name, departments.department_name

     FROM employees

     LEFT JOIN departments ON employees.department_id = departments.department_id;

RIGHT JOIN

   – SELECT employees.first_name, departments.department_name

     FROM employees

     RIGHT JOIN departments ON employees.department_id = departments.department_id;

Filtering Results

IN

   – SELECT * FROM employees WHERE department_id IN (1, 2, 3);

See also  7 Businesses Under the Ownership of Google's Parent Company, Alphabet

BETWEEN

   – SELECT * FROM employees WHERE salary BETWEEN 40000 AND 60000;

LIKE

   – SELECT * FROM employees WHERE first_name LIKE ‘J%’;

NULL Values

   – SELECT * FROM employees WHERE manager_id IS NULL;

Grouping Data

GROUP BY

   – SELECT department_id, COUNT(*) FROM employees GROUP BY department_id;

HAVING

   – SELECT department_id, COUNT(*) FROM employees GROUP BY department_id HAVING COUNT(*) > 5;

Updating Data

UPDATE

   – UPDATE employees SET salary = salary * 1.1 WHERE department = ‘IT’;

DELETE

   – DELETE FROM employees WHERE department = ‘HR’;

Conclusion

SQL is a versatile language that empowers you to manage and manipulate data effectively.

With the above 50 SQL query examples, you now have a foundation for handling various data-related tasks. Whether you’re retrieving data, aggregating information, joining tables, filtering results, grouping data, or updating records, SQL proves to be an indispensable tool in working with relational databases.

As you delve deeper into SQL, you’ll discover even more powerful features and techniques to unlock the full potential of your data management endeavors. Happy querying!

How useful was this post?

Click on a star to rate it!

As you found this post useful,

Please share this to social media platforms

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?



Leave a Reply

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