Home > Database > SQL > body text

How to write descending order in sql

下次还敢
Release: 2024-05-08 10:57:16
Original
347 people have browsed it

Descending sorting can be achieved in SQL by using the DESC keyword. Syntax: SELECT column_name(s) FROM table_name ORDER BY column_name DESC; For example, to sort employees in descending order by the salary column: SELECT name, salary FROM employees ORDER BY salary DESC.

How to write descending order in sql

Descending sort in SQL

In SQL, the method to implement descending sort is very simple, you can use Keyword DESC.

Syntax:

<code class="sql">SELECT column_name(s)
FROM table_name
ORDER BY column_name DESC;</code>
Copy after login

Example:

Suppose we have the following table:

<code class="sql">CREATE TABLE employees (
    id INT NOT NULL AUTO_INCREMENT,
    name VARCHAR(255) NOT NULL,
    salary INT NOT NULL,
    PRIMARY KEY (id)
);</code>
Copy after login

The following query Will return all employees in descending order by column salary:

<code class="sql">SELECT name, salary
FROM employees
ORDER BY salary DESC;</code>
Copy after login

Notes:

  • ##DESC Key Words can only be used in ORDER BY clauses.
  • You can sort multiple columns in descending order by separating the column names with commas and appending
  • DESC after each column name. For example:
<code class="sql">SELECT name, salary
FROM employees
ORDER BY salary DESC, name DESC;</code>
Copy after login
This will sort the results by the

salary column in descending order, and if the salary are equal, then by the name# in descending order ## Column to sort the results.

The above is the detailed content of How to write descending order in sql. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template