Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SQL – How to order by two columns

Imagine we have a table ‘user’ with several different column. Two of them are date (date) and isValid (boolean). I would like to write sql what sort this by two columns (date and isValid). First of all i would like to sort by date ASC, then every row what has isValid = 1 should be after all row with isValid = 0. So even if have a row with date = 2022.01.01 with isValid = 0 should be before row with date 2021.01.01

Initial Data:

Date       IsValid
2023         0
2022         1
2025         0
2024         1
2026         0

Expected Data:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Date    IsValid
2023      0
2025      0
2026      0
2022      1
2024      1

>Solution :

With a comma between the columns…

SELECT
    date,
    isValid
FROM
    yourTable
ORDER BY
    isValid,
    date
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading