![]() ![]() When you read the statement, you know your record set is ordered in descending order and then ordered in ascending order by last name. The ASC phrase is used in the above statement for clarity. The following SQL statement is an example. You separate columns by appending a comma and then adding another column parameter in the ORDER BY statement. The result would give you a list of people grouped by the customer's state and then ordered by their last names. For instance, you might want to sort your records based on state and then last name. SQL lets you order records based on multiple columns. The following code is how you write your DESC SQL statement. Taking the same data set as you used with the ASC order statement, let's reverse the customers. The DESC or DESCENDING keyword lists data in descending order. You can also list data in descending order. Your data is still stored without the sorting, but the SELECT statement shows you the following data set. Notice the difference is the ASC because it's implied when you eliminate it from your SQL statements. The following SQL statement is the same as the above statement. Ascending order is set by default but you could also add the "ASC" keyword to your statement. In the above statement, your data is returned and alphabetically sorted by state. You can sort your data by state using the following SQL statement. If you have thousands of customers and you want to see a list of customers in a specific state without excluding any other states, it would be too difficult to browse through your data without any type of sorting ability. The following is your current list of customers. SQL lets you sort alphabetically, numerically or chronologically.įor instance, suppose you want to get a list of your customers, and you need the list in alphabetical order by state. You can sort records in ascending or descending order, and you can sort records based on multiple columns. SQL uses the ORDER BY statement to sort records. In addition to sorting, you also use filtering options to return only specific records that match your requirements. There is no guarantee that records are returned in a specific order when you don't use sorting options in SQL. In most cases, the SQL server returns records in the same order they are added to the database. Now, our table has the following records with duplicate names.When you run a SELECT query without any sorting options, the SQL server returns the records in an indiscriminate order. INSERT INTO COMPANY VALUES (9, 'James', 44, 'Norway', 5000.00 ) Now, let us create three more records in COMPANY table using the following INSERT statements. Sqlite> SELECT NAME, SUM(SALARY) FROM COMPANY GROUP BY NAME If you want to know the total amount of salary on each customer, then GROUP BY query will be as follows − ExampleĬonsider COMPANY table with the following records. Make sure whatever column you are using to group, that column should be available in the column-list. You can use more than one column in the GROUP BY clause. GROUP BY clause must follow the conditions in the WHERE clause and must precede ORDER BY clause if one is used. Syntaxįollowing is the basic syntax of GROUP BY clause. ![]() GROUP BY clause follows the WHERE clause in a SELECT statement and precedes the ORDER BY clause. SQLite GROUP BY clause is used in collaboration with the SELECT statement to arrange identical data into groups. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |