BA Prince the B.A. Subscribe

Home / SQL for Business Analysts

SQL SELECT Basics: Get Data Out of a Table (Beginner Tutorial)

SQL SELECT Basics: Get Data Out of a Table (Beginner Tutorial)

Watch on YouTube Subscribe

The SELECT statement is how you get data out of any table. Learn SELECT star, picking columns, renaming them with AS, and adding a calculated column, on a sample online shop database.

Step by step

Select

  1. Get data out. SELECT is how you get data out of a table in SQL. The examples here use a sample database for an online shop.
  2. The customers table. We start with the customers table. The column list on the left shows its columns, such as first_name, last_name, email and city.

Select star

  1. SELECT star. Type SELECT * FROM customers; on 2 lines. The * means every column, FROM names the table, and the semicolon marks the end of the query. Run it. You get every column for every customer, which is handy for a first look at a table.
    SELECT *
    FROM customers;
  2. Forty rows. The result has 40 rows. Reports rarely need every column, and on a big table SELECT * can be slow because it pulls back data you don't need.

Pick columns

  1. Pick columns. Instead, name the columns you need. Replace the * with first_name, last_name, city, with a comma between each name but none after the last one. Run it. You get the same 40 customers, but only 3 columns, which is much easier to read.
    first_name, last_name, city

Rename with AS

  1. Rename with AS. Column names from a database can be hard to read. AS gives a column a new name in the result, so change last_name to last_name AS surname. Then run the query again.
    last_name AS surname
  2. The new name. The column header now says surname. The customers table itself didn't change; only this result uses the new name.

Calculated column

  1. Calculate. SELECT can also calculate. Clear the editor and query the products table: unit_price * 1.2 gives the price with 20% tax, and AS with_tax names the new column. Run it. The result has 24 rows, one per product, each with its unit_price and its with_tax price.
    SELECT product_name, unit_price,
      unit_price * 1.2 AS with_tax
    FROM products;
  2. Price with tax. The with_tax column isn't stored in the database. SQL works it out each time you run the query, so it always uses the current unit_price.

Recap

  1. SELECT in four moves. In short: SELECT * shows every column, naming columns keeps the result tidy, AS renames them, and you can calculate new ones. The next step is filtering rows with WHERE.

The order SQL runs a SELECT statement in

You write SELECT first, but the database doesn't run it first. It starts with FROM to find the table, then WHERE to keep the rows that match, then SELECT to pick and work out the columns, and ORDER BY last to sort them.

This explains a classic beginner error. If you write SELECT unit_price * 1.2 AS with_tax FROM products WHERE with_tax > 10;, most databases say with_tax doesn't exist, because WHERE runs before SELECT has made that name (SQLite is an exception and allows it). Repeat the sum instead: WHERE unit_price * 1.2 > 10. ORDER BY runs after SELECT, so ORDER BY with_tax works fine.

Take a quick look at a big table

SELECT * is fine for a first look, but on a table with millions of rows it pulls back far more than you need. Ask for a few rows instead. In MySQL, PostgreSQL and SQLite, add LIMIT at the end: SELECT * FROM customers LIMIT 10;. SQL Server uses TOP instead: SELECT TOP 10 * FROM customers;. Oracle uses FETCH FIRST 10 ROWS ONLY at the end (Oracle 12c and later).

Without ORDER BY, the database can give you any 10 rows, and they may not be the same next time. That's fine for seeing what the columns look like. If you want the top 10 of something, sort first, as the lesson SQL ORDER BY and LIMIT: Top 10 Lists shows.

Where the other parts of a SELECT go

Every other part you learn next fits into the same query, always in this order: SELECT, then FROM, then WHERE, then GROUP BY, then HAVING, then ORDER BY, and LIMIT last. You only add the parts you need, but the ones you use must stay in that order, or you get a syntax error.

Each part has its own job. WHERE filters rows, GROUP BY makes totals per group, HAVING filters those totals, ORDER BY sorts, and LIMIT keeps only the first few rows. SELECT DISTINCT removes repeated rows from the result. The lessons SQL WHERE Clause: Filter Rows Like an Analyst, SQL GROUP BY Explained (Totals per Category), SQL ORDER BY and LIMIT: Top 10 Lists and SQL DISTINCT: Remove Duplicate Rows each take one part, step by step.

Common mistakes

  • A comma after the last column. SELECT first_name, last_name, city, FROM customers; gives a syntax error in most databases (such as MySQL, PostgreSQL, SQL Server and SQLite). Put commas only between column names so your query works everywhere.
  • A missing comma between 2 columns. SELECT first_name last_name FROM customers; often runs without an error, but SQL reads last_name as a new name for first_name. You get 1 column called last_name full of first names, so check every gap has a comma.
  • Typing the column name as it sounds. first name with a space is not the same as first_name, and the query fails. Copy the exact names from the column list, underscores included.
  • Thinking AS renames the column in the table. AS only changes the header in this one result. The table still has a column called last_name, so other queries must still use that name.

When a business analyst uses this

During user acceptance testing of a checkout screen, a business analyst can run SELECT product_name, unit_price, unit_price * 1.2 AS with_tax FROM products; and compare the with_tax column against the prices the screen shows. Any product that doesn't match goes in the defect log before sign-off.

Questions people ask

What is a basic SELECT statement in SQL?

It is SELECT, then the columns you want, then FROM and the table name, like SELECT first_name, last_name, city FROM customers;. Use * instead of column names to get every column.

What are some simple SQL SELECT examples?

SELECT * FROM customers; shows every column, and SELECT last_name AS surname FROM customers; renames a column in the result. SELECT product_name, unit_price * 1.2 AS with_tax FROM products; adds a calculated column.

How do WHERE and ORDER BY fit into a SELECT query?

They both go after FROM. WHERE keeps only the rows that meet a condition, and ORDER BY sorts the result. When you use both, WHERE comes first.

More in SQL for Business Analysts