Home | Categories

50 SQL Queries Examples

Register: Online Office Site | Tech Training

Advertisement

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

Advertisement

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’;

See also  Demystifying SQL Injection: Understanding the Threat and How to Protect Your Website

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);

BETWEEN

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

See also  SQL Data Types for MS SQL Server, MySQL and MS Access

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.

See also  SQL 101: Everything You Need to Know About Structured Query Language

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!

Categories: SQL


Disclaimer for nTells Online Tech. - All the information on this website - https://ntells.com - is published in good faith and for general information purpose only. nTells Online Blog does not make any warranties about the completeness, reliability and accuracy of this information. Any action you take upon the information you find on this website (nTells Online Blog), is strictly at your own risk. nTells Online Blog will not be liable for any losses and/or damages in connection with the use of our website.

From our website, you can visit other websites by following hyperlinks to such external sites. While we strive to provide only quality links to useful and ethical websites, we have no control over the content and nature of these sites. These links to other websites do not imply a recommendation for all the content found on these sites. Site owners and content may change without notice and may occur before we have the opportunity to remove a link which may have gone 'bad'.

Please be also aware that when you leave our website, other sites may have different privacy policies and terms which are beyond our control. Please be sure to check the Privacy Policies of these sites as well as their "Terms of Service" before engaging in any business or uploading any information.

Consent - By using our website, you hereby consent to our disclaimer and agree to its terms.

Leave a Reply

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






Online Office | Pricing | Advertise

mcfaidesigns