SELECT Queries and Functions

Harry · 11 Sep 2026 · 8 views

Selecting Data

SELECT name, email FROM customers;
SELECT * FROM customers WHERE city = 'Mumbai' ORDER BY name;
SELECT DISTINCT city FROM customers;

NULL Handling With NVL and NVL2

SELECT name, NVL(city, 'Not specified') AS city_label
FROM customers;

SELECT NVL2(city, city, 'No city') FROM customers;

NVL returns the fallback when the first value is NULL; NVL2 returns different values for NULL and non-NULL.

Pagination With ROWNUM and OFFSET

-- Classic top-N query
SELECT * FROM (
  SELECT name FROM customers ORDER BY name
) WHERE ROWNUM <= 10;

-- Modern row-limiting clause (12c+)  
SELECT name FROM customers ORDER BY name
OFFSET 20 ROWS FETCH NEXT 10 ROWS ONLY;

String and Number Functions

SELECT UPPER(name), LENGTH(name), ROUND(total, 2),
       TO_CHAR(created_at, 'DD-MON-YYYY') AS fmt
FROM customers, orders;

Key Points

  • WHERE filters, ORDER BY sorts, DISTINCT deduplicates.
  • NVL and NVL2 turn NULLs into friendly values.
  • Use FETCH FIRST/OFFSET for clean pagination in 12c+.
  • TO_CHAR and TO_DATE convert between dates and text.
Share this post:

Comments (0)

Please login or register to comment.