SQL (Structured Query Language) is a programming language used for managing and manipulating relational databases. It provides several clauses that can be used to specify conditions, filter data, sort results, and perform various operations. Here are some commonly used SQL clauses:
- SELECT: The SELECT clause is used to retrieve data from one or more database tables. It specifies the columns to be included in the result set.
- FROM: The FROM clause is used to specify the table or tables from which the data should be retrieved. It defines the source of the data for the query.
- WHERE: The WHERE clause is used to specify conditions that filter the data based on a specific criterion. It is used to retrieve only the rows that satisfy the given condition(s).
- GROUP BY: The GROUP BY clause is used to group rows based on one or more columns. It is often used in conjunction with aggregate functions like SUM, AVG, COUNT, etc., to perform calculations on grouped data.
- HAVING: The HAVING clause is used to filter the groups created by the GROUP BY clause. It specifies conditions that filter the grouped data based on aggregate values.
- ORDER BY: The ORDER BY clause is used to sort the result set based on one or more columns. It can sort the data in ascending (ASC) or descending (DESC) order.
- JOIN: The JOIN clause is used to combine rows from two or more tables based on a related column between them. It allows you to retrieve data from multiple tables using a single query.
- INSERT INTO: The INSERT INTO clause is used to insert new rows into a table. It specifies the table name and the values to be inserted into the corresponding columns.
- UPDATE: The UPDATE clause is used to modify existing data in a table. It allows you to update one or more columns with new values based on specified conditions.
- DELETE FROM: The DELETE FROM clause is used to delete rows from a table. It specifies the table name and conditions that determine which rows should be deleted.
These are just some of the commonly used SQL clauses. SQL provides many more clauses and keywords that allow you to perform various operations and manipulate data in a relational database.
Example: Sorting Results in Ascending Order
Sure! Here’s an example of sorting results in ascending order using the ORDER BY clause in SQL:
Let’s assume we have a table called “Employees” with the following structure and sample data:
Table: Employees +—-+———-+———–+ | ID | Name | Salary | +—-+———-+———–+ | 1 | John | 3000 | | 2 | Mary | 2500 | | 3 | David | 3500 | | 4 | Sarah | 4000 | +—-+———-+———–+
To retrieve the employees’ data sorted by their salary in ascending order, you would use the following SQL query:
SELECT * FROM Employees ORDER BY Salary ASC;
The result of this query would be:
+—-+———-+———–+ | ID | Name | Salary | +—-+———-+———–+ | 2 | Mary | 2500 | | 1 | John | 3000 | | 3 | David | 3500 | | 4 | Sarah | 4000 | +—-+———-+———–+
The ORDER BY clause is used to specify the column(s) by which you want to sort the result set. In this case, we sorted the result set by the “Salary” column in ascending order (ASC). If you wanted to sort in descending order, you would use the DESC keyword instead:
SELECT * FROM Employees ORDER BY Salary DESC;
This would give you the following result:
+—-+———-+———–+ | ID | Name | Salary | +—-+———-+———–+ | 4 | Sarah | 4000 | | 3 | David | 3500 | | 1 | John | 3000 | | 2 | Mary | 2500 | +—-+———-+———–+
As you can see, the result set is now sorted by the “Salary” column in descending order (from highest to lowest).
Example: Sorting Results in Descending Order
Certainly! Here’s an example of sorting results in descending order using the ORDER BY clause in SQL:
Let’s assume we have a table called “Products” with the following structure and sample data:
Table: Products +—-+————+———+ | ID | ProductName | Price | +—-+————+———+ | 1 | Laptop | 1200 | | 2 | Smartphone | 800 | | 3 | TV | 1500 | | 4 | Headphones | 100 | +—-+————+———+
To retrieve the products’ data sorted by their price in descending order, you would use the following SQL query:
SELECT * FROM Products ORDER BY Price DESC;
The result of this query would be:
+—-+————+———+ | ID | ProductName | Price | +—-+————+———+ | 3 | TV | 1500 | | 1 | Laptop | 1200 | | 2 | Smartphone | 800 | | 4 | Headphones | 100 | +—-+————+———+
The ORDER BY clause is used to specify the column(s) by which you want to sort the result set. In this case, we sorted the result set by the “Price” column in descending order (DESC). The products are displayed from the highest price to the lowest.
If you wanted to sort the result set in ascending order, you would use the ASC keyword:
SELECT * FROM Products ORDER BY Price ASC;
This would give you the following result:
+—-+————+———+ | ID | ProductName | Price | +—-+————+———+ | 4 | Headphones | 100 | | 2 | Smartphone | 800 | | 1 | Laptop | 1200 | | 3 | TV | 1500 | +—-+————+———+
As you can see, the result set is now sorted by the “Price” column in ascending order (from lowest to highest).