# Learn SQL

> Learn SQL from scratch with real examples: SELECT, WHERE, GROUP BY and JOIN on a students table, plus a 6-week practice plan and free official sources.

![Learn SQL](https://tiswa.app/assets/blog/learn-sql/learn-sql-cover-en.webp)

## What is SQL and why does everyone ask for it?

SQL stands for Structured Query Language, a language for working with relational databases: data lives in tables, each table has columns and rows, and tables link to each other. Systems such as PostgreSQL, MySQL, SQLite and SQL Server all understand SQL, with small differences in the details. What you learn on one system carries over to the rest.

| Your major | Why you need SQL |
|---|---|
| Computer science and software engineering | Every web or mobile app needs a database, and your graduation project likely does too |
| Information systems | Designing and querying databases is a core part of the courses |
| Data analysis and statistics | Most company data sits in databases, and SQL is the first tool to pull it |
| Business, finance and marketing | Sales and customer reports start with a query, and knowing it saves you waiting on others |

![Comparison card of the guide table](https://tiswa.app/assets/blog/learn-sql/learn-sql-inline-2-en.webp)


## Set up in five minutes

The easiest start is SQLite. According to its official site, it is a complete database library in a single file with no server and no configuration. Install a free tool that opens SQLite files, or use the sqlite3 command line, and create a new file. Then paste this code to build a small students table:

Create a table and add data:

```
CREATE TABLE students (
  id INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  major TEXT,
  gpa REAL,
  city TEXT
);

INSERT INTO students (name, major, gpa, city) VALUES
  ('Sara', 'Computer Science', 4.6, 'Jeddah'),
  ('Faisal', 'Accounting', 3.9, 'Riyadh'),
  ('Noura', 'Computer Science', 4.1, 'Riyadh'),
  ('Omar', 'Nursing', 4.8, 'Jeddah'),
  ('Lama', 'Accounting', 3.2, 'Dammam');
```

> **Tip**: Write keywords such as SELECT and FROM in capitals, and table and column names in lower case. SQL does not require it, but it makes a query easier to read.

## The core queries

These queries cover most of what you will write in your first month. Run each one and predict the result before you see it, because that is the fastest way to understand the language:

Select, filter and sort:

```
-- every student
SELECT * FROM students;

-- computer science students only
SELECT name, gpa
FROM students
WHERE major = 'Computer Science';

-- highest GPAs first
SELECT name, gpa
FROM students
ORDER BY gpa DESC
LIMIT 3;
```

SELECT picks the columns, FROM picks the table, WHERE filters the rows, ORDER BY sorts and LIMIT sets how many. You can combine conditions with AND and OR, and use LIKE to search text.

Grouping:

```
-- average GPA and number of students per major
SELECT major,
       COUNT(*) AS students,
       ROUND(AVG(gpa), 2) AS avg_gpa
FROM students
GROUP BY major
HAVING COUNT(*) >= 2
ORDER BY avg_gpa DESC;
```

GROUP BY gathers similar rows, and functions such as COUNT, AVG, SUM and MAX compute over each group. The key difference: WHERE filters rows before grouping, and HAVING filters groups after it.

## Linking tables with JOIN

In a real database you do not put everything in one table. Students go in one table, courses in another, and enrolments in a third that links them. This is where JOIN shows its power:

JOIN two tables:

```
CREATE TABLE enrollments (
  student_id INTEGER REFERENCES students(id),
  course TEXT,
  grade INTEGER
);

INSERT INTO enrollments VALUES
  (1, 'Databases', 95), (1, 'Algorithms', 88),
  (3, 'Databases', 79), (4, 'Anatomy', 97);

SELECT s.name, e.course, e.grade
FROM students s
JOIN enrollments e ON e.student_id = s.id
WHERE e.course = 'Databases';
```

A plain JOIN returns only rows that match in both tables. A LEFT JOIN returns every student, even those with no enrolment, and their enrolment columns show as empty (NULL). Try both and compare.

## A six-week plan

1. **Week 1: tables and selecting**: CREATE TABLE, INSERT, SELECT, WHERE and ORDER BY on the students table. Write twenty queries yourself.
2. **Week 2: grouping**: COUNT, AVG, SUM, GROUP BY and HAVING. Ask questions about your data: which city has the highest average?
3. **Week 3: JOIN**: Design three linked tables for a topic you know, such as a library or a small shop, and write queries that join them.
4. **Week 4: changes and constraints**: UPDATE, DELETE, primary and foreign keys and NOT NULL. Learn why you need WHERE before any DELETE.
5. **Week 5: real data**: Finish the free [Kaggle Intro to SQL](https://www.kaggle.com/learn/intro-to-sql) course, which gives a completion certificate and uses real datasets.
6. **Week 6: a small project**: Design a database for an app you like, write ten queries that answer useful questions, and push it to GitHub with a README.

![5-step flow: Week 1: tables and selecting / Week 2: grouping / Week 3: JOIN / Week 4: changes and constraints / Week 5: real data](https://tiswa.app/assets/blog/learn-sql/learn-sql-inline-1-en.webp)


## Free official sources

- [The SQLite SQL language docs](https://www.sqlite.org/lang.html): a reference for every command with examples and diagrams.
- [The PostgreSQL tutorial](https://www.postgresql.org/docs/current/tutorial.html): explains tables, queries and joins clearly, and suits you when you move to a server database.
- [Kaggle Intro to SQL](https://www.kaggle.com/learn/intro-to-sql), then [Advanced SQL](https://www.kaggle.com/learn/advanced-sql): two free courses with hands-on exercises and a completion certificate.
- JetBrains tools are free for students and include database tools. Find them in [free software for students](https://tiswa.app/en/blog/free-software-for-students/).

## Let AI coach you

Instead of letting it write the query, ask it for exercises on your table and to correct your attempts. Try this with [ChatGPT](https://tiswa.app/en/blog/chatgpt-for-students/) or [Gemini](https://tiswa.app/en/blog/gemini-free-for-students/):

SQL coach:

```
I am learning SQL. Here is my table: students(id, name, major, gpa, city) and enrollments(student_id, course, grade). Give me 5 practice questions in plain English, from easy to hard, one at a time. Wait for my query, then tell me if it is correct, show the result it would return, and explain any mistake without rewriting the whole query.
```

tiswa gathers free student tools for coding and data, such as the GitHub pack and JetBrains tools, and every student discount in Saudi Arabia in one place. See [computer science deals](https://tiswa.app/en/deals/majors/computer-science/) and verify once. [Join tiswa](https://tiswa.app/en/join/)

## Common mistakes

- Running DELETE or UPDATE without WHERE, which changes every row. Write a SELECT with the same condition first and check the result.
- Using = to compare with NULL. The correct form is IS NULL or IS NOT NULL.
- Putting a condition on a COUNT result in WHERE instead of HAVING.
- Using SELECT * in every query. Pick only the columns you need.
- Keeping all data in one huge table instead of splitting it into linked tables.

SQL is the first step in the [data analysis roadmap](https://tiswa.app/en/blog/data-analysis-roadmap/), and you need it with [JavaScript](https://tiswa.app/en/blog/learn-javascript/) when you build an app that stores user data. If you study computer science, also read [AI for computer science students](https://tiswa.app/en/blog/ai-for-computer-science-students/).

## Questions

### Where do I start learning SQL?

Start with SQLite on your computer because it needs no server and no setup, create a small table, then learn SELECT, WHERE, GROUP BY and JOIN in that order.

### How long does it take to learn SQL basics?

At an hour a day, four to six weeks covers the core queries, grouping, joins and a small project. Advanced queries come later with practice.

### Is SQL a programming language?

It is a query language built for databases. You do not build a full app with it alone, but it works with any programming language, such as Python and JavaScript.

### Should I learn MySQL, PostgreSQL or SQLite?

The basics are the same in all three. Start with SQLite because it is simple, then move to PostgreSQL or MySQL when you build an app that runs on a server.

### Do I need SQL for data analysis?

Yes. Most company data is stored in databases, and SQL is the first tool you use to pull data before analysing it.


## Sources

[^1]: sqlite.org, https://www.sqlite.org/about.html
[^2]: sqlite.org, https://www.sqlite.org/lang.html
[^3]: postgresql.org, https://www.postgresql.org/docs/current/tutorial.html
[^4]: kaggle.com, https://www.kaggle.com/learn/intro-to-sql
[^5]: kaggle.com, https://www.kaggle.com/learn/advanced-sql
