SELECT — Fetching Data
Write your first real SQL query and learn how to choose exactly which columns and rows you want back.
SELECT is the most used command in SQL. Everything starts here — choose columns, pick the table, get your data.
EXERCISE
1Your First SELECT Query
SELECT is how you ask a database for data. Every query starts here. Learning to write clear, precise SELECT statements is the foundation of everything else.
The basic structure:
SELECT column1, column2
FROM table_name;
Fetching all columns with * :
-- Get every column from every row in products:
SELECT * FROM products;
-- Get only the columns you need (much better practice):
SELECT name, price, stock_qty
FROM products;
Why avoid SELECT * in real work:
-- SELECT * problems:
-- 1. Returns columns you don't need (wastes bandwidth)
-- 2. Breaks code if table structure changes (new column added)
-- 3. Harder to read — no way to tell which columns you're using
-- Always prefer explicit column lists:
SELECT id, name, price
FROM products;
Column aliases — renaming output columns:
SELECT
name AS product_name,
price AS price_usd,
stock_qty AS units_available
FROM products;
-- Output header will show: product_name | price_usd | units_available
-- instead of: name | price | stock_qty
Simple calculations in SELECT:
SELECT
name,
price,
price * 1.18 AS price_with_gst, -- 18% GST
price * 0.90 AS discounted_price -- 10% off
FROM products;
Pro Tip: The AS keyword is optional —
price * 1.18 AS price_with_gstandprice * 1.18 price_with_gstboth work. But always use AS for readability.
EXERCISE
2String Functions in SELECT
MySQL has built-in functions for transforming text directly inside your query — so you can clean, format, and combine strings without any extra code.
Most useful string functions:
SELECT
UPPER(name) AS name_upper, -- 'arjun' → 'ARJUN'
LOWER(email) AS email_lower, -- 'PRIYA@...' → 'priya@...'
LENGTH(name) AS name_length, -- number of characters
TRIM(name) AS name_trimmed, -- remove leading/trailing spaces
SUBSTRING(name, 1, 3) AS first_3_chars, -- 'Arjun' → 'Arj'
CONCAT(first_name, ' ', last_name) AS full_name,
REPLACE(phone, '-', '') AS phone_clean -- remove dashes from phone
FROM customers;
CONCAT — combining columns:
-- Build a full display name:
SELECT CONCAT(first_name, ' ', last_name) AS full_name
FROM employees;
-- Build a formatted address:
SELECT CONCAT(city, ', ', state, ' ', pincode) AS address
FROM customers;
LIKE — pattern matching in SELECT or WHERE:
-- % matches any sequence of characters
-- _ matches exactly one character
SELECT name FROM products
WHERE name LIKE 'Phone%'; -- starts with 'Phone'
SELECT name FROM products
WHERE name LIKE '%Pro%'; -- contains 'Pro' anywhere
SELECT code FROM products
WHERE code LIKE 'SKU-___-2024'; -- exactly 3 chars between dashes
Pro Tip: String functions do not change the stored data — they only change what is displayed in the query result. The original values in the table are untouched.
EXERCISE
3Date Functions in SELECT
Date and time are some of the most common data types you will work with. MySQL has powerful built-in functions to extract, calculate, and format them.
Getting the current date and time:
SELECT NOW(); -- '2024-07-15 14:30:00' (date + time)
SELECT CURDATE(); -- '2024-07-15' (date only)
SELECT CURTIME(); -- '14:30:00' (time only)
Extracting parts from a date:
SELECT
order_date,
YEAR(order_date) AS yr, -- 2024
MONTH(order_date) AS mo, -- 7
DAY(order_date) AS dy, -- 15
DAYNAME(order_date) AS day_name, -- 'Monday'
MONTHNAME(order_date) AS month_name -- 'July'
FROM orders;
Date calculations:
-- Days between order and today:
SELECT
id,
order_date,
DATEDIFF(NOW(), order_date) AS days_ago
FROM orders;
-- Add/subtract from a date:
SELECT
order_date,
DATE_ADD(order_date, INTERVAL 30 DAY) AS due_date,
DATE_SUB(order_date, INTERVAL 1 MONTH) AS prev_month
FROM orders;
Formatting dates for display:
SELECT DATE_FORMAT(order_date, '%d %M %Y') AS formatted
FROM orders;
-- Output: '15 July 2024'
SELECT DATE_FORMAT(order_date, '%Y-%m-%d') AS iso_format
FROM orders;
-- Output: '2024-07-15'
Pro Tip: Always store dates as DATE or DATETIME, never as VARCHAR. Stored as text, you lose sorting, arithmetic, and all these functions.
