…
…
SQL and Database Fundamentals · Lesson 1 / 41 · Introduction to databases
PreviewIn this lesson you will
You can read the explanation without an account. The exercises and quiz at the end of the lesson are for members.
You started as an intern in the student affairs office at Northbridge University in Port Alder. For years the office kept student records in separate spreadsheets; now everything lives in one database. Your first job is to pull the list of departments from the database yourself.
In this lesson you will write your first SQL query without installing anything. By the end you will be able to fetch the list of departments, the names of the instructors, or any other table you like from the student affairs database. First we will see why this database exists, then how you ask it questions.
In this lesson you will:
SELECT * FROM department;.Let's separate two words before we start. Data is recorded facts: a student's name, birth date, or midterm score in a course. When you process data to answer a question, you get information: the answer to "How many students are in Business Administration?" is information. The database stores data; you ask it questions and get information out.
For small jobs, a spreadsheet or a text file works fine. If you track your own expenses or the grade sheet of one course by yourself, you need nothing else. The problems start when the data grows and several people work with the same data. In student affairs, four of them happened every day.
The first is inconsistency. A student named Maya Brooks appeared in five separate files: the enrollment list, the scholarship list, the advising list, the dormitory list, and the graduation tracker. A staff member noticed a typo in her last name and fixed it in one file. The other four files kept the old spelling. Now nobody knows which one is right. The second is working at the same time: if two staff members open the same file and both save, the one who saves last erases the other's changes. The third is permissions: only the instructor should see grades and only student affairs should see addresses, but everyone who opens the file sees everything. The fourth is searching: finding the answer to "Which students failed three courses?" by hand in thousands of rows takes hours.
Student affairs keeps the same student's information in five separate files. A staff member fixed a typo in the last name in only one of them. What is this problem called?
A database is a place where related data is stored in an organized way. The software that manages a database is called a database management system (DBMS). A DBMS does five jobs: it stores data in one place, rejects data that breaks the rules (for example a birth date of February 31), protects users who work at the same time from each other, controls who can see what, and answers the questions you ask. The most common DBMSs are PostgreSQL, MySQL, SQLite, Microsoft SQL Server, and Oracle. A bank's account transactions, an online shop's orders, a university's student information system, even the contacts app on your phone all live in a database.
So the difference is not only size. A database is not "a very big spreadsheet"; it is a system with rules, serving many people at once and answering questions. Below, match each file problem with what the DBMS offers.
Match each file problem with the solution the DBMS gives for it.
For each row, pick its match from the list, then check.
You ask the DBMS questions in SQL (Structured Query Language). People say it either "S-Q-L" or "sequel"; both are correct. SQL is a declarative language: you say what you want, and the DBMS decides how to find it.
Let's see the difference with an example. You want to find the students in Business Administration. In a programming language such as Python you describe it step by step: "Go through the student rows one by one; in each row look at the department; if it is Business Administration, add the row to a list; at the end, show the list." In SQL you only describe the result: "Get the students whose department is Business Administration." The system chooses which order to read the rows in and whether to use a shortcut. You will write the SQL version of this sentence in lesson 4; for now the idea is enough.
SQL is a "declarative" language. What does this mean?
SQL is not only a tool for programmers. A manager who pulls their own report, a marketer who looks at campaign results, a hospital employee who organizes the appointment system, or a founder who builds their own app all use the same language. Knowing basic SQL means finding the answer to your own question in almost any job that works with data, without waiting for someone else.
What happens behind the scenes when you write a query and run it? Put the steps below in the order they should happen.
Put what happens when you run a query in order.
Click the lines in order; to undo one you placed wrong, click it in the list below.
Shuffled lines
Your order
In a database, data lives in tables. Each table has a name; each row holds a record (a department, a student) and each column holds one property of that record (the name, the faculty). The student affairs database has seven tables; you will get to know them one by one in the next lesson. Today we start with the smallest: the department table, which holds the departments.
Here is the first query:
SELECT * FROM department;
| id | name | faculty | founded_year |
|---|---|---|---|
| 1 | Computer Programming | School of Applied Technology | 2010 |
| 2 | Software Engineering | Faculty of Engineering | 2012 |
| 3 | Industrial Engineering | Faculty of Engineering | 1999 |
| 4 | Business Administration | Faculty of Business and Management | 1999 |
| 5 | Management Information Systems | Faculty of Business and Management | 2008 |
| 6 | New Media and Communication | Faculty of Communication | 2014 |
| 7 | Psychology | Faculty of Humanities and Social Sciences | 2005 |
| 8 |
(8 rows)
Let's read the query piece by piece. SELECT means "get these". The asterisk (*) means "all columns". FROM department says which table the data comes from. The semicolon (;) at the end shows that the command is finished. In plain English: "From the department table, get all columns." The result has eight rows because the university has eight departments, and four columns because the table has four columns.
Upper and lower case do not matter in SQL keywords: select * from department; gives the same result. Still, there is a common habit: write keywords (SELECT, FROM) in upper case and table and column names in lower case. That way you can see at a glance which part of the query is a command and which part is the name of your data.
One more assurance: in this course the database runs directly in your browser, so there is nothing to install. Each query runs on a copy of the data and is rolled back afterwards. On top of that, SELECT only reads; it does not change, delete, or break the table. Try as much as you like.
Try it now· not graded; doesn't affect progress
Now make two small experiments. Write course instead of department and run it: how many rows came back? (There are 33 courses.) Then write term: which academic terms are there? (You will see three terms.) Each time you only changed the table name; the rest of the query stayed the same.
Most of the time you do not need every column. A list that goes to department heads does not need the id or the founding year; the department name and faculty are enough. Instead of the asterisk, you write the names of the columns you want, separated by commas:
SELECT name, faculty FROM department;
| name | faculty |
|---|---|
| Computer Programming | School of Applied Technology |
| Software Engineering | Faculty of Engineering |
| Industrial Engineering | Faculty of Engineering |
| Business Administration | Faculty of Business and Management |
| Management Information Systems | Faculty of Business and Management |
| New Media and Communication | Faculty of Communication |
| Psychology | Faculty of Humanities and Social Sciences |
| Data Science and Analytics | Faculty of Engineering |
(8 rows)
The number of rows did not change; there are still eight departments. What changed is the number of columns: two instead of four. The column list is like the items on an order; what you write is what comes. You must write column names exactly as they are in the table. In this database, names contain no spaces or capital letters, and words are joined with an underscore: the column for the founding year is named founded_year.
The same pattern works on every table. Let's get the instructors' first name, last name, and title:
SELECT first_name, last_name, title FROM instructor;
Result (five of 20 rows):
| first_name | last_name | title |
|---|---|---|
| Diego | Costa | Associate Professor |
| Lucía | Kowalski | Lecturer |
| Rosa | Bianchi | Lecturer |
| Olivia | Osei | Professor |
| Liam | Reyes | Assistant Professor |
(20 rows)
Who decides the order of the columns in the result: the table or you? Predict.
In the department table the columns are stored in the order id, name, faculty, founded_year. What does the result of this query look like?
SELECT faculty, name FROM department;
Now it is your turn. The query below returns the students' first name, last name, and city. First write year instead of city to get which year of study the students are in. Then write the same query for instructors with the columns first_name, last_name, title.
Try it now· not graded; doesn't affect progress
SELECT is the most used SQL command, but not the only one. SQL commands are grouped by what they do, and the units of this course follow these groups. In the first five units you only read data; then you build tables yourself, and at the end you change data and decide who can access what.
| Group | Commands | What it does | In this course |
|---|---|---|---|
| Querying | SELECT | Reads data | U1–U5: query basics, aggregation, joins, advanced queries |
| Definition (DDL) | CREATE, ALTER, DROP | Builds and changes tables | U6: database design |
| Data manipulation (DML) | INSERT, UPDATE, DELETE | Adds, updates, and deletes rows | U7: data management |
| Access control (DCL) | GRANT, REVOKE | Decides who can access what | U7: data management |
Which group does each command belong to?
For each row, pick its match from the list, then check.
SELECTCREATEINSERTGRANTALTERDELETEOnce you learn SQL, you use it in every database. Think of a library app. The books are in a table called book; its columns are id, title, author, pages, and year. To see the title and author of the books on the shelf, you use the same pattern you wrote in student affairs; only the table and column names change:
SELECT title, author FROM book;
| title | author |
|---|---|
| The Midnight Library | Matt Haig |
| The Alchemist | Paulo Coelho |
| Pride and Prejudice | Jane Austen |
| Dune | Frank Herbert |
| The Little Prince | Antoine de Saint-Exupery |
| Things Fall Apart | Chinua Achebe |
(6 rows)
A music app, a café, or a hospital: it makes no difference. As long as you know the table name and the columns, SELECT ... FROM ...; works the same way.
SQL is not a new invention. In 1970, Edgar F. Codd, who worked at IBM, published the relational model, which proposed storing data in tables of rows and columns. In 1974, again at IBM, a language called SEQUEL was developed to ask questions of this model; its name was later shortened to SQL. In 1986 SQL became a standard for the first time, and PostgreSQL got its current name in 1996. A lot has changed in more than fifty years, but the basic commands you will write today are almost the same as in those days. SQL also works like a calculator; if you write an operation without a table, it gives you the result:
SELECT 2026 - 1970;| ?column? |
|---|
| 56 |
(1 row)
56 years have passed since Codd's paper. The column name came out as ?column?, because we did not give a name to the calculated value; you will learn to name columns in lesson 3.
The standard offers a common base, but every DBMS adds its own extensions on top of it. These differences are called dialects. If a query you find on the internet was written for another DBMS, it may not work in PostgreSQL.
You found this query on the internet, written for Microsoft SQL Server. Why does it give an error when you run it in PostgreSQL?
SELECT TOP 3 first_name FROM student;
The most common error beginners meet is misspelling a table name. The table is named student; if you write the plural students, which feels natural in English, or make a typo such as studnet, PostgreSQL does not run the query:
OutputERROR: relation "students" does not existOutputERROR: relation "studnet" does not existRead the message word by word: "There is no relation called students." In PostgreSQL, relation means table. So the DBMS is telling you "I could not find such a table". Below the error message, the site also shows a "What does this mean?" explanation. When you see this message, compare the name you wrote with the name in the database letter by letter. The table names in this course are singular (student, course, department) and written in lower case with underscores.
Changing the table name is enough. The term table holds each term's name, start date, and end date:
SELECT * FROM term;
| id | name | start_date | end_date |
|---|---|---|---|
| 1 | 2025-Fall | 2025-09-22 | 2026-01-16 |
| 2 | 2026-Spring | 2026-02-16 | 2026-06-12 |
| 3 | 2026-Fall | 2026-09-21 | 2027-01-15 |
(3 rows)
The catalog needs only the course code, name, and credits:
SELECT code, name, credits FROM course;
Result (four of 33 rows):
| code |
|---|
Forgetting the comma between column names. For the request "Get the name and founding year of the departments", this is written:
SELECT name founded_year FROM department;
Result (three of 8 rows):
| founded_year |
|---|
| Computer Programming |
| Software Engineering |
| Industrial Engineering |
(8 rows)
The query does not give an error, but the result is wrong: one column comes back, its header is founded_year, and it contains department names. Without the comma, PostgreSQL takes the second word not as a new column but as a name given to the first column. The correct version is SELECT name, founded_year FROM department;. If the number of columns in the result does not match the number of names you wrote, check the commas first.
SELECT only reads and does not change the table.SELECT * FROM table; returns all columns, and SELECT column, column FROM table; returns only the columns you wrote, in the order you wrote them; table and column names are written as they are in the database (student, founded_year).Next lesson: you will get to know each of the seven tables by querying it, and find the key that tells every row apart.
Exercises and quiz are for members
This lesson has 6 coding exercises and a 10-question quiz. Sign up free to solve them, save your progress and move on to the next lesson.
| Faculty of Engineering |
| 2026 |
| credits |
|---|
| CPR101 | Algorithms and Programming I | 4 |
| CPR102 | Algorithms and Programming II | 4 |
| CPR103 | Databases I | 3 |
| CPR104 | Databases II | 3 |
(33 rows)
There are three names in the query and three columns in the result. The number of rows is the number of courses in the table.