SQL Guide Chapter

Lesson 2 – Retrieving Data Using the SQL SELECT Statement (or: how to politely interrogate your database)

And look, the SELECT statement is the thing you thought SQL was: “show me stuff from a table.” But it also hides a surprising number of ways to confuse yourself with aliases, NULLs, and operator precedence.

oracle 19c mysql notes study chapter

And look, the SELECT statement is the thing you thought SQL was: “show me stuff from a table.” But it also hides a surprising number of ways to confuse yourself with aliases, NULLs, and operator precedence.

In this lesson you will learn to:


1. SELECT Basics (Alex just wants Accounting)

Picture Alex: they want a list of employees in the Accounting department. Somewhere there’s an HR app where they choose department = Accounting and click Go, and magically a report appears.

Behind that magic is a SELECT statement, which:

Syntax basics:

Example formatting:

SELECT last_name,
           salary,
           hire_date,
           manager_id
    FROM   employees;

Most tools (like SQL Developer) have a Format command that:

Use it. Frequently.


2. SELECT and FROM: All Columns vs Specific Columns

2.1 Selecting all columns

The laziest (and sometimes useful) form:

SELECT *
    FROM   employees;

2.2 Selecting specific columns

When you know what you want, list the columns explicitly:

SELECT last_name,
           job_id,
           salary
    FROM   employees;

Rules:


3. Running SELECT in Different Tools

3.1 SQL Developer

Column heading defaults:

3.2 SQL*Plus

3.3 MySQL Workbench

3.4 MySQL Command‑Line Client


4. The DUAL Table and Constant Expressions

Sometimes you don’t want rows from a table; you just want a single calculated value, like today’s date.

In Oracle, that’s where the DUAL table comes in:

Example – using SYSDATE:

-- This returns today's date once for every employee
    SELECT first_name,
           SYSDATE
    FROM   employees;
    
    -- This returns today's date exactly once
    SELECT SYSDATE
    FROM   dual;

In MySQL:


5. Arithmetic Expressions and Operator Precedence

You can use arithmetic operators directly in the SELECT list:

Operators:

Example – monthly salary vs annual salary:

SELECT last_name,
           salary,
           salary * 12    annual_salary
    FROM   employees;

Now suppose you want to give everyone a hypothetical $100/month raise and see the new annual salary:

SELECT last_name,
           salary * 12,
           salary * 12 + 100    wrong_annual,
           (salary + 100) * 12  corrected_annual
    FROM   employees;

Why two columns?

Moral: when in doubt about precedence, use parentheses instead of trusting your memory.


6. NULL: The Four‑Meaning Troublemaker

NULL in SQL is not 0, not an empty string, and not “we’ll decide later.” It generally means one of:

Example:

Critical rule:

Example – annual salary including commission:

SELECT last_name,
           salary,
           salary * 12
             + salary * 12 * commission_pct   annual_with_commission
    FROM   employees;

Later, you’ll see functions to substitute default values for NULL (like “treat NULL commission as 0”). For now, just remember: NULL poisons arithmetic.


7. Column Aliases

Column aliases let you rename the column heading in your result set:

Examples:

-- Simple alias
    SELECT last_name AS name,
           salary    AS monthly_salary
    FROM   employees;
    
    -- Alias without AS (still works)
    SELECT commission_pct commission
    FROM   employees;
    
    -- Alias with spaces and mixed case (quotes required)
    SELECT salary * 12 AS "Annual Salary"
    FROM   employees;

Without quotes, aliases default to uppercase in most tools. With double quotes, you get exactly the casing and spaces you specify.


8. Concatenation, Literals, and the CONCAT Function

8.1 The concatenation operator (||)

In Oracle, || combines strings (character expressions):

-- First and last name with a space
    SELECT first_name || ' ' || last_name AS full_name
    FROM   employees;

Notes:

SELECT first_name || ' earns ' || salary AS pay_info
    FROM   employees;

You’ll see everything left‑aligned, including the number, because it’s now text.

8.2 CONCAT function (Oracle vs MySQL)

MySQL example:

SELECT CONCAT(first_name, ' ', last_name, ' is a ', job_id)
    FROM   employees;

Oracle equivalent (nesting CONCAT):

SELECT CONCAT(CONCAT(first_name, ' '), last_name) AS full_name
    FROM   employees;

Or, much simpler in Oracle: just use ||.

8.3 Literal character strings

A literal is a hard‑coded value in your SELECT list, like a word or phrase.

Example:

SELECT last_name || ' is a ' || job_id AS description
    FROM   employees;

This produces rows like “Abel is a SA_REP”.


9. Alternative Quote Operator (Oracle) and Escapes (MySQL)

What if your literal includes an apostrophe, like isn't? If you write this naively:

SELECT 'King isn't happy' AS msg
    FROM   dual;

Oracle sees the ' in isn't and panics.

9.1 Oracle’s alternative quote operator

Use q followed by a single quote and a pair of delimiters:

SELECT q'[King isn't happy]' AS msg
    FROM   dual;

Pattern:

Just don’t use & as a delimiter – that triggers substitution variables and a whole different adventure.

9.2 MySQL string escapes

In MySQL, you typically escape the single quote:

SELECT 'King isn\'t happy' AS msg;

or depending on configuration:

SELECT 'King isn''t happy' AS msg;

Either way: you’re telling the engine “this quote is part of the text, not the end of it.”


10. DISTINCT: Getting Rid of Duplicates

Sometimes you don’t care about every row – you just want unique values.

Example:

SELECT department_id
    FROM   employees;

You’ll see many repeated department IDs. To shrink it down:

SELECT DISTINCT department_id
    FROM   employees;

11. DESCRIBE / DESC: Seeing Table Structure

When you’re not sure what’s in a table, DESCRIBE is your friend.

In Oracle (SQL*Plus or SQL Developer script output):

DESCRIBE employees;
    -- or shorter
    DESC employees;

You’ll see:

In SQL Developer’s GUI:

In MySQL Workbench:


12. What You Should Now Be Able to Do

By the end of this lesson, you should be able to:

If that feels like a lot for one lesson, remember: this is still just reading data. Writing and changing it comes later, and that’s when things get really exciting—or really broken—depending on how carefully you’ve learned this part.

← previous view source markdown next →