Beginner SQL: Select Data with Simple Queries

Step 1: Understanding Programming Basics

Objective: Familiarize yourself with programming concepts and workflows.

Programming means giving instructions to a computer. SQL (Structured Query Language) is used to interact with databases and obtain information from them efficiently.

Databases are organized systems to store and manage large amounts of data.

Step 2: Installing Required Tools

Objective: Install the tools needed for writing and running SQL queries.

First, download a code editor like VS Code or Notepad++. Then install a SQL engine. SQLite is recommended because it's small, free, and works on any computer.

Follow instructions from the official download page for each tool.

Step 3: Running SQL Commands

Objective: Learn how to use your tools to enter and run SQL queries.

To use SQLite, open your terminal and type sqlite3. Inside SQLite, you can write SQL statements to interact with the database.


-- Start SQLite
sqlite3 mydatabase.db

-- Exit SQLite
.exit
content_copy

Step 4: What is a Database Table?

Objective: Understand how data is organized in tables.

Think of a table as a way to organize related data. Each row holds a single item or record, and each column is a property of that item.


-- Example of a table named 'students':
+----+--------+-----+
| id | name   | age |
+----+--------+-----+
| 1  | Alice  | 21  |
| 2  | Bob    | 22  |
+----+--------+-----+
content_copy

Step 5: Creating a Sample Table

Objective: Learn how to create your own database table.

Use the CREATE TABLE command to make a table, then INSERT INTO to add some example records. Run these in your SQL tool.


CREATE TABLE students (
  id INTEGER PRIMARY KEY,
  name TEXT,
  age INTEGER
);

INSERT INTO students (name, age) VALUES ('Alice', 21);
INSERT INTO students (name, age) VALUES ('Bob', 22);
INSERT INTO students (name, age) VALUES ('Carol', 20);
content_copy

Step 6: Basic SELECT Queries

Objective: Retrieve all data from a table using the SELECT statement.

To view all data, use SELECT * FROM followed by the table name. The star (*) means 'all columns'.


SELECT * FROM students;
content_copy

You should see all rows and columns from the 'students' table.

Step 7: Selecting Specific Columns

Objective: Choose exact columns to display from a table.

Type the column names you want after SELECT. Commas separate different columns.


SELECT name, age FROM students;
content_copy

This will show only the 'name' and 'age' values for each student record.

Step 8: Filtering Data with WHERE

Objective: Limit which rows appear using conditions.

Add WHERE to your SELECT statement to specify conditions. SQL will return only rows that match.


SELECT * FROM students WHERE age = 21;
content_copy

You will get only the rows where age is 21.

Step 9: Using Comparison Operators

Objective: Apply comparisons to filter for greater than, less than, etc.

Try changing the number and operator. For example, to find students older than 20:


SELECT name, age FROM students WHERE age > 20;
content_copy

Step 10: Combining Conditions with AND and OR

Objective: Use multiple conditions in queries.

To find students named 'Alice' who are older than 20:


SELECT * FROM students WHERE name = 'Alice' AND age > 20;
content_copy

Or to find students named 'Alice' or anyone aged 22:


SELECT * FROM students WHERE name = 'Alice' OR age = 22;
content_copy

Step 11: Sorting Results with ORDER BY

Objective: Sort your data in ascending or descending order.

To list students from youngest to oldest:


SELECT * FROM students ORDER BY age ASC;
content_copy

To list from oldest to youngest:


SELECT * FROM students ORDER BY age DESC;
content_copy

Step 12: Limiting Results with LIMIT

Objective: Restrict the number of rows returned by your query.

Combine LIMIT and ORDER BY to control what and how much you see:


SELECT * FROM students ORDER BY age DESC LIMIT 2;
content_copy

This shows only the two oldest students in the table.

Step 13: Reading Simple Query Errors

Objective: Identify and fix common typing mistakes in queries.

If you type a column name wrong, you'll see an error. For example:


SELECT nam FROM students;
-- Error: no such column: nam
content_copy

Make sure every keyword and column name is spelled correctly.

Step 14: Summary and Next Steps

Objective: Recap and explore further learning options.

Congratulations! You can now select and filter data with SQL.

Continue by learning to change data (INSERT, UPDATE, DELETE) and advanced topics like combining multiple tables. If you want to show data on a website, consider learning web development alongside SQL.