SQL SELECT for Beginners: Choose Columns and Name Your Results

SQL TutorialsBy QueryFun4 min read
On this page
  1. Start with a precise question
  2. Understand rows, columns and results
  3. Give output columns clear names
  4. Calculate a new value
  5. When SELECT star helps, and when it gets in the way
  6. Try it yourself

A database table can contain far more information than you need. SQL SELECT lets you ask for the specific columns that answer your question. In this lesson, you will turn an everyday request into a readable query and understand each part of the result.

Practice setup: These lessons use SQLite and the fictional shop database from the QueryFun SQL practice lab. Open that lab, run its setup once in a fresh practice database, then return here. Do not run the INSERT statements again for each lesson. You can use SQLite Fiddle without installing a database server.

The customers are Amina (1, Nairobi), Brian (2, Mombasa) and Chao (3, Nairobi). Orders are 101 (customer 1, amount 1200), 102 (customer 2, amount 800), 103 (customer 1, amount 600) and 104 (customer 3, amount 1500). Amounts are fictional currency units. Each order belongs to one customer. A customer can have several orders.

Start with a precise question

Suppose someone asks, “Which customers do we have, and where do they live?” You need the name and city columns from customers. You do not need order amounts yet. Keeping the question small makes it easier to tell whether your answer is right.

SELECT name, city
FROM customers;

SELECT introduces the columns you want to see. FROM names the table that supplies them. A comma separates columns. The semicolon ends the statement. Line breaks and indentation make the query easier to read, but they do not change its meaning.

The result contains Amina with Nairobi, Brian with Mombasa and Chao with Nairobi. Without ORDER BY, SQL does not promise a particular row order. Check the values rather than assuming the first row will always be Amina.

Understand rows, columns and results

A row represents one record. A column describes one kind of information about that record. In customers, customer_id identifies the customer, name stores a name and city stores a city. Selecting two columns does not remove the third column from the stored table. It only changes what this query returns.

Try swapping the requested columns to SELECT city, name FROM customers;. You still get the same customers, but the city appears first. That can be useful when preparing a report for someone who expects a particular layout.

Give output columns clear names

An alias is a temporary label for a column in the result. It helps readers understand a report without changing the database structure.

SELECT name AS customer_name,
       city AS customer_city
FROM customers;

The result headings are now customer_name and customer_city. AS makes the purpose explicit. An alias does not rename the original column. A later query must still use the stored name unless it defines its own alias.

Calculate a new value

You can also select an expression. Imagine you need a rough whole-unit discount illustration using half of each order amount. Division by 2.0 makes the intention to calculate a fractional value clear in SQLite.

SELECT order_id, amount,
       amount / 2.0 AS half_amount
FROM orders
ORDER BY order_id;

The half_amount values are 600.0, 400.0, 300.0 and 750.0 for orders 101 through 104. This is a calculated result, not a new saved column. Real pricing rules need agreed rounding and tax rules; this example simply demonstrates arithmetic.

When SELECT star helps, and when it gets in the way

SELECT * FROM customers; returns all columns. It is handy while exploring a small unfamiliar practice table. For a report you plan to reuse, list the columns explicitly. Your intention stays visible, and an extra column added later will not unexpectedly appear in the result.

If a query fails, check the spelling of the table and each column. Text values belong in single quotes, but ordinary column names in these examples do not. SELECT 'name' FROM customers; returns the literal word name for each row, not the customers' names.

Try it yourself

Write a query that returns order_id first and amount second. Rename the second result column original_amount. Sort by order_id so you can compare your answer consistently. Then add a calculated column called doubled_amount without modifying any stored records.

Check your answer

Use this query:

SELECT order_id,
       amount AS original_amount,
       amount * 2 AS doubled_amount
FROM orders
ORDER BY order_id;

You should see 101, 1200, 2400; 102, 800, 1600; 103, 600, 1200; and 104, 1500, 3000. There are four rows because the input has four orders. If you see twelve rows, check that you did not accidentally add customers to FROM without a join condition.

Keep learning: Return to the SQL learning path to choose your next lesson.

Further reading: SQLite SELECT documentation.

Leave a Reply

Your email address will not be published. Required fields are marked *