TECHNOLOGIESTieInn Technologies

SELECT basics: columns, aliases, DISTINCT, LIMIT

SQL · 13 questions

Short notes

Key idea

SELECT picks columns, FROM names the table. Every query you write starts here. You can rename a column with AS, compute new ones, and cut the output with LIMIT.

Syntax

SELECT col1, col2 AS new_name, price * qty AS total
FROM table_name
LIMIT 10;
  • SELECT * returns every column. Fine for a quick look, not for real queries.
  • DISTINCT after SELECT removes duplicate rows.

Example

Show each order's id and its value with tax:

SELECT order_id, amount, amount * 1.18 AS amount_with_tax
FROM orders
LIMIT 5;

The new column exists only in this result; the table is unchanged.

Traps

  • DISTINCT applies to the whole row, not just the first column after it.
  • In standard SQL an alias can't be used in WHERE, because WHERE runs before SELECT. DuckDB allows it; PostgreSQL, MySQL and SQL Server don't, so in interviews repeat the expression.
  • Strings use single quotes; double quotes are for column names.
  • Rows have no fixed order without ORDER BY, so LIMIT alone gives "some" rows, not "the first".

Want the full lesson with step-by-step examples, a project and a certificate? SQL course, ₹120 →

Questions

Open in Code Lab5 free questions
5 questions

    SELECT basics: columns, aliases, DISTINCT, LIMIT

  1. Question 1easy

    Which query renames price to unit_price in the result?

  2. SELECT basics: columns, aliases, DISTINCT, LIMIT

  3. Question 2medium

    What does SELECT DISTINCT city, is_premium FROM customers return?

  4. SELECT basics: columns, aliases, DISTINCT, LIMIT

  5. Question 3medium

    There are 40 customers. 3 have no city recorded, and the rest live in 9 different cities. What does this return?

    SELECT COUNT(DISTINCT city) FROM customers;
  6. SELECT basics: columns, aliases, DISTINCT, LIMIT

  7. Question 4easy

    80 orders were placed by customers. How many rows does this return?

    SELECT COUNT(*) FROM (SELECT DISTINCT customer_id FROM orders);
  8. SELECT basics: columns, aliases, DISTINCT, LIMIT

  9. Question 5medium

    What does SELECT product_name price FROM products; return? (Notice there's no comma.)

8 more questions on this topic, with hints, and tests

Sign up free for 21 days of every question and test.