0
0
PostgreSQLquery~5 mins

ORDER BY with NULLS FIRST and NULLS LAST in PostgreSQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does ORDER BY column NULLS FIRST do in PostgreSQL?
It sorts the results by the specified column, placing all NULL values at the beginning of the sorted list before non-NULL values.
Click to reveal answer
beginner
How does ORDER BY column NULLS LAST affect the sorting order?
It sorts the results by the specified column, placing all NULL values at the end of the sorted list after non-NULL values.
Click to reveal answer
beginner
By default, where do NULL values appear when sorting in ascending order without specifying NULLS FIRST or NULLS LAST?
In ascending order, NULL values appear last by default.
Click to reveal answer
beginner
By default, where do NULL values appear when sorting in descending order without specifying NULLS FIRST or NULLS LAST?
In descending order, NULL values appear first by default.
Click to reveal answer
beginner
Write a PostgreSQL query to select all rows from a table employees ordered by salary ascending, but with NULL salaries shown first.
SELECT * FROM employees ORDER BY salary ASC NULLS FIRST;
Click to reveal answer
In PostgreSQL, what does ORDER BY age DESC NULLS LAST do?
ASorts by age descending, placing NULLs at the end
BSorts by age ascending, placing NULLs at the beginning
CSorts by age descending, placing NULLs at the beginning
DSorts by age ascending, placing NULLs at the end
What is the default position of NULLs when sorting ascending without specifying NULLS FIRST or LAST?
AAt the beginning
BAt the end
CRandomly placed
DDepends on the data type
Which clause explicitly places NULL values at the start when sorting?
ANULLS LAST
BNULLS TOP
CNULLS FIRST
DNULLS BEGIN
If you want NULLs to appear last in descending order, which clause should you use?
ANULLS LAST
BNo clause needed
CNULLS FIRST
DORDER BY NULLS LAST DESC
What happens if you omit NULLS FIRST or NULLS LAST in an ORDER BY clause?
ANULLs are ignored
BNULLs are always first
CQuery fails with error
DDefault NULL placement depends on ASC or DESC
Explain how PostgreSQL handles NULL values in ORDER BY clauses and how NULLS FIRST and NULLS LAST change this behavior.
Think about where NULLs appear by default and how the clauses move them.
You got /3 concepts.
    Write a query to order a table by a column with NULL values shown first, and explain why you might want to do this.
    Use ORDER BY column ASC NULLS FIRST.
    You got /2 concepts.