SQL Tutorial menu

SQL Introduction

SQL is the language you use to work with data stored in a relational database.

What is SQL?

  • SQL stands for Structured Query Language
  • It lets you read data from a database
  • It lets you add, change and delete data
  • It lets you create new databases and tables
  • It is an international standard (ANSI and ISO), so the same basic commands work in MySQL, PostgreSQL, SQL Server, Oracle and SQLite

Many people pronounce it "S-Q-L", and many say "sequel". Both are fine.

What is a Relational Database?

A relational database keeps data in tables. A table looks like a sheet in Excel:

  • Each column holds one kind of information, such as a name or a city
  • Each row (also called a record) holds the information about one thing, such as one student
  • Tables can be related to each other through shared values, which is where the word "relational" comes from

The software that stores the tables and runs your SQL is called a DBMS (Database Management System). MySQL, PostgreSQL and SQL Server are all examples.

The Sample Database

This tutorial uses one small database for an IT institute. It has two tables.

The students table:

idnamecityagemarkscourse_id
1Ali KhanKarachi19781
2Ayesha SiddiquiLahore21852
3Bilal AhmedKarachi22641
4Fatima NoorIslamabad20923
5Hamza RazaKarachi1870NULL
6Sana IqbalLahore23882

The courses table:

idtitleduration_months
1Web Development6
2Python Programming4
3Graphic Designing3
4Digital Marketing3

The course_id column in students points to the id column in courses. So Ali Khan (course_id 1) studies Web Development. Hamza Raza has NULL, which means "no value": he has not chosen a course yet.

Keep these two tables in mind. Every example in the following lessons runs on exactly this data, so you can check each result by looking back here.

Your First Query

A query is a request sent to the database. This one asks for every row and every column of the courses table:

Example

sql
SELECT * FROM courses;
idtitleduration_months
1Web Development6
2Python Programming4
3Graphic Designing3
4Digital Marketing3

The * means "all columns". This query asks only for the names of the courses that last 3 months:

Example

sql
SELECT title FROM courses WHERE duration_months = 3;
title
Graphic Designing
Digital Marketing

Even without any training, you can almost read it as English: "select the title from courses where the duration is 3 months".

What Can SQL Do?

TaskSQL command
Read dataSELECT
Add new rowsINSERT
Change rowsUPDATE
Remove rowsDELETE
Create a tableCREATE TABLE
Change a tableALTER TABLE
Remove a tableDROP TABLE

Where Do I Run SQL?

You need a database system to run SQL. Good free options are MySQL, PostgreSQL and SQLite. Online playgrounds, such as DB Fiddle, also let you type a query in the browser. The commands in this tutorial are standard SQL and run on all of them.

Exercise

Exercise

In a database table, what is a row?

Learn SQL with a teacher

Join the Full Stack Development course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp