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.
Write a query in SQL and press Enter.
Finds the data and sends the answer back.
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.
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:
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.
->. 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.
| Group | Full name | What it does | Commands |
|---|---|---|---|
| DDL | Data Definition Language | Builds and changes the structure — the database and the tables themselves | create, alter, drop |
| DML | Data Manipulation Language | Changes the data inside the tables | insert, update, delete |
| DQL | Data Query Language | Asks questions and gets data back, changing nothing | select |
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
Structured Query Language — the language used to give instructions to an RDBMS.
One instruction written in SQL.
The output a query returns, laid out as a table.
Ends a query. Without it MySQL waits, showing -> instead of running.
What does SQL stand for?
The output returned by a query is called…
You type a query, press Enter, and see -> instead of mysql>. What happened?
Which group does create table belong to?