What is SQL?
SQL, or Structured Query Language, is a programming language used for managing and manipulating relational databases.
What’s a relational database? It might sound fancy, but a relational database is just a collection of data tables, each of which consists of rows and columns. For example, here’s what a simple relational database used by a school might look like:
The database contains three tables:
- The first table,
students, contains information about all the students enrolled at the school. Each row represents a student, and each column represents an attribute of that student (e.g., their age, name, or class teacher).
- The next table,
teachers, contains information about all the teachers who work at the school.
- The final table,
payroll, contains information about salary payments made to the teachers at the school
The reason we refer to this as a relational database instead of “a bunch of tables” is because of the way the tables are stored and linked - or related - together. If you look closely at the database schema (diagram) above, you’ll spot a couple lines which symbolise these relationships:
- First, we can see that there is a line drawn between the
teacherstables. This line is used to show that the
studentstable is linked to the
teacherstable via the
teacher_idcolumn, which functions as a foreign key. This might sound fancy, but all it means is that each value in the
teacher_idcolumn of the
studentstable corresponds to a value in the
teacher_idcolumn of the
- Second, we can see that the
teacherstable is also linked to the
payrolltable. Each teacher has a
payroll_idattribute; this column functions as the foreign key linking the
payrolltables. Once again, we draw a line in our database schema to represent this link.
If the concept of “foreign keys” sounds a bit abstract at the moment, don’t worry. As we move through this course, we’ll give plenty of examples and revisit the concept often. By the end of the course, I promise that this will all be “easy as pie”, as we say in the UK.
What’s SQL got to do with relational databases?
Everything! SQL is a coding language used specifically for interacting with relational databases.
For example, here’s a simple SQL query which fetches some data from the
students table in our school database:
SELECT id, name, age
WHERE id = 5
Can you read it? Sure! Do you understand it? Not yet... but you will.
If we were to run this query in our database, the relational database management system (RDBMS) will fetch the
age attributes for the student in the
students table whose
id is equal to 5 and return a table like this:
As you can see, SQL syntax isn’t all that different from the English syntax we use in everyday speech. It uses simple keywords like
WHERE. This is one of the nice features of SQL; it’s a very high level programming language which uses easily understandable terms.