SELECT basics
SELECT chooses which columns to read from a table.
In this page:
Syntax
SELECT column1, column2 AS alias
FROM table_name;
SELECT basics
SELECT * returns every column and SELECT col1, col2 returns just those. AS gives a column or expression an alias, and expressions like price * qty are computed per row. FROM names the table. Prefer listing columns instead of * in real applications.
Note:
SELECT * is fine for exploring but avoid it in production queries.
Example: SELECT basics
CREATE TABLE products (id INTEGER PRIMARY KEY, name TEXT, price INTEGER, qty INTEGER);
INSERT INTO products VALUES (1, 'Pen', 3, 100), (2, 'Notebook', 8, 40), (3, 'Lamp', 45, 5);
SELECT * FROM products;
SELECT name, price FROM products;
SELECT name, price * qty AS stock_value FROM products;
-- Output:
-- id | name | price | qty
-- 1 | Pen | 3 | 100
-- 2 | Notebook | 8 | 40
-- 3 | Lamp | 45 | 5
-- name | price
-- Pen | 3
-- Notebook | 8
-- Lamp | 45
-- name | stock_value
-- Pen | 300
-- Notebook | 320
-- Lamp | 225
Login to try C/C++/Java/PHP code in the editor
Related Topics
Common Mistakes
- Using SELECT * everywhere
- Forgetting the FROM clause
- Confusing single and double quotes
Chapter Summary
- SELECT columns FROM table
- * means all columns
- AS creates aliases
- Expressions are computed per row
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: