Getting started with SQL

Tables, rows, and the practice data used in these lessons.

A relational database stores data in tables of rows and columns. SQL is the language for reading and changing that data. It works in much the same way in MySQL, PostgreSQL, Oracle and SQL Server.

Every lesson uses these two tables. To follow along, paste this into a free online SQL editor or a local database.

SQL
CREATE TABLE students (
  id INTEGER PRIMARY KEY,
  name VARCHAR(50) NOT NULL,
  dept VARCHAR(10),
  marks INTEGER
);

CREATE TABLE enrolments (
  student_id INTEGER REFERENCES students(id),
  course VARCHAR(50)
);

INSERT INTO students VALUES
  (1, 'Asha', 'CSE', 91),
  (2, 'Bala', 'IT', 74),
  (3, 'Chitra', 'CSE', 83),
  (4, 'Dinesh', 'ECE', 58),
  (5, 'Esther', 'IT', 88);

INSERT INTO enrolments VALUES
  (1, 'Python'),
  (1, 'Java'),
  (3, 'SQL'),
  (5, 'Python');

Your first query

SQL
SELECT name, marks FROM students ORDER BY id;
Output
Asha | 91
Bala | 74
Chitra | 83
Dinesh | 58
Esther | 88
Filtering and sorting โ†’