0Pricing
SQL Academy · Lesson

Selecting Columns and Expressions

Project columns, compute derived expressions, alias results with AS, and use DISTINCT to remove duplicates.

Selecting Columns and Expressions is a free SQL Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the SQL Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The Basic SELECT

SELECT projects columns from one or more tables.

SELECT id, email, full_name
FROM users;

SELECT *

SELECT * returns every column. Convenient in the REPL, bad in production code:

  • Returns more bytes than you need
  • Breaks when columns are added or reordered
  • Hides intent in code review

Computed Expressions

You can compute new columns in the SELECT list:

SELECT id,
       price,
       quantity,
       price * quantity AS subtotal,
       UPPER(email) AS email_upper
FROM order_items;

Column Aliases with AS

AS renames the output column. The keyword is optional but improves readability:

SELECT first_name || ' ' || last_name AS full_name,
       EXTRACT(YEAR FROM created_at) AS signup_year
FROM users;

String Concatenation

Standard SQL concatenates with ||. Many databases also support CONCAT(...):

SELECT first_name || ' ' || last_name FROM users;
SELECT CONCAT(first_name, ' ', last_name) FROM users;

-- ||  with NULL → NULL
-- CONCAT skips NULLs (treats them as empty strings)

DISTINCT

DISTINCT removes duplicate rows from the result:

SELECT DISTINCT country FROM users;
SELECT DISTINCT country, city FROM users;  -- distinct pairs

DISTINCT ON (PostgreSQL)

DISTINCT ON keeps the first row per group, defined by ORDER BY:

-- Most recent login per user:
SELECT DISTINCT ON (user_id) user_id, login_at, ip
FROM logins
ORDER BY user_id, login_at DESC;

CASE Expressions in SELECT

Compute one of several values per row:

SELECT id,
       total,
       CASE
         WHEN total >= 1000 THEN 'whale'
         WHEN total >=  100 THEN 'regular'
         ELSE 'small'
       END AS bucket
FROM orders;

Quoting Identifiers vs Strings

SQL uses two kinds of quotes:

-- Single quotes: string literal
SELECT 'hello' AS greeting;

-- Double quotes: identifier (column/table name with special chars or case)
SELECT "First Name" FROM users;

-- Mixing them up is a common error.

Type Casting

Convert values between types with :: or CAST(... AS ...):

SELECT '2024-01-15'::DATE AS d;
SELECT CAST('42' AS INTEGER) AS n;
SELECT (price::TEXT || ' USD') AS pretty FROM products;

Selecting from Multiple Tables (Preview)

You can pull columns from several tables in one query — that's what JOINs are for. We'll cover them in the JOIN Basics course.

SELECT u.email, o.total
FROM users u
JOIN orders o ON o.user_id = u.id;

Recap

SELECT projects columns and expressions.

  • Use explicit column lists, not *
  • Compute on the fly with expressions
  • Alias with AS for clarity
  • DISTINCT removes duplicate output rows

Quick Check

Which keyword removes duplicate rows from a result set?

Frequently asked questions

Is the “Selecting Columns and Expressions” lesson free?

Yes — the full text of “Selecting Columns and Expressions” is free to read here on the web, and the SQL Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the SQL Academy course, upgrade to CoddyKit PRO.

What will I learn in “Selecting Columns and Expressions”?

Project columns, compute derived expressions, alias results with AS, and use DISTINCT to remove duplicates. You practise SQL Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start SQL Academy?

No prior experience is required. SQL Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Selecting Columns and Expressions” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this SQL Academy lesson?

Yes. Every SQL Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Selecting Columns and Expressions
  2. WHERE Filters: Comparison and Logic Operators
  3. ORDER BY Single and Multiple Columns
  4. LIMIT, OFFSET and Pagination Basics
← Back to SQL Academy