LambdaLabTM
Databases & SQL · Class 12 · Database Concepts
databasesSQL⏱️ 8 min read

SQL — Talking to the Database

You have a relational database and an RDBMS looking after it. Now you need a way to ask it things. You cannot open the files and read them yourself — you have to send the software an instruction, in a language it understands. That language is SQL.

1What SQL is

SQL is the language used to operate and give instructions to RDBMS applications.

Structured Query Language

An instruction written in SQL is called a query. You send it to the RDBMS; the RDBMS does the work and sends back an answer. Nothing else passes between you.

You

Write a query in SQL and press Enter.

query →← resultset
MySQL

Finds the data and sends the answer back.

SQL is not a programming language like Python
In Python you say how: loop over the list, test each item, keep the ones that match. In SQL you say what: “the students in class 12”. Working out how to find them is the RDBMS's job, not yours. That is why one line of SQL can replace a dozen lines of a loop.

2A first query

Here is a real one, run against the students table this course uses throughout. Do not worry about writing it yet — just look at the shape of the exchange.

MySQL command line client
mysql> select name from students;
+--------+
| name |
+--------+
| Veena |
| Anjali |
| Sohan |
| Lhamu |
| Ravi |
| Karma |
| Diana |
| Bhim |
| Tom |
| Pema |
| Farhan |
| Nima |
+--------+
12 rows in set (0.00 sec)

You asked for one column out of a table of twelve students, and got exactly that. The query reads almost like English, which is deliberate: SQL was designed so that people who were not programmers could use it.

3The answer is called a resultset

The output of a query has a name worth learning, because questions use it: the resultset.

The resultset is the output a query returns.

A resultset is itself laid out as a table — rows and columns, drawn in that box of + and | characters. It is not stored anywhere. It is built to answer your question, shown to you, and thrown away.

The last line always tells you how many rows came back, and how long it took:

MySQL command line client
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| lambdalab |
| mysql |
| performance_schema |
| practice |
| sys |
+--------------------+
6 rows in set (0.01 sec)
The databases you did not create
information_schema, mysql, performance_schema and sys come with MySQL and are how it keeps track of itself. Leave them alone; lambdalab is the one made for this course.

4Every query ends with a semicolon

The semicolon is how you say “I have finished typing, run it”. Press Enter without one and MySQL does not run anything — it assumes there is more to come and waits, showing an arrow instead of the usual prompt.

That is a feature, not a nuisance: it lets one long query be spread over several lines so it stays readable.

one query, three lines — nothing runs until the ;
mysql> select name
-> from students
-> where grade = 12;
The commonest beginner moment
You press Enter, nothing happens, and the prompt has changed to ->. Nothing is broken and nothing is frozen. You simply have not typed the semicolon yet. Type ; and press Enter, and the query runs.

5The parts of SQL

SQL commands are sorted into groups by the kind of job they do. You do not have to memorise every command in the table below — you will meet them one at a time — but you should be able to say which group a command belongs to.

The sub-languages of SQL
GroupFull nameWhat it doesCommands
DDLData Definition LanguageBuilds and changes the structure — the database and the tables themselvescreate, alter, drop
DMLData Manipulation LanguageChanges the data inside the tablesinsert, update, delete
DQLData Query LanguageAsks questions and gets data back, changing nothingselect
Where select belongs
Some books count select as part of DML rather than giving DQL a name of its own, and the CBSE Computer Science syllabus lists only DDL and DML. Either answer is accepted; what matters is that you can say what select does — it reads data without changing it.

6Recap

SQL

Structured Query Language — the language used to give instructions to an RDBMS.

Query

One instruction written in SQL.

Resultset

The output a query returns, laid out as a table.

The semicolon

Ends a query. Without it MySQL waits, showing -> instead of running.

Quick Check

What does SQL stand for?

Quick Check

The output returned by a query is called…

Quick Check

You type a query, press Enter, and see -> instead of mysql>. What happened?

Quick Check

Which group does create table belong to?