1. Home
  2. Explore
  3. Computer Science
  4. SQL Basics

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

SQL Basics

SQL Study Notes

What is SQL?

SQL (Structured Query Language) is a language used to communicate with databases. It is similar to a programming language like Python, but its primary purpose is to interact with databases — creating, reading, updating, and deleting data. SQL is an ISO-standardized language, meaning the core syntax works across most database systems.

SQL is the standard language for relational database management and manipulation.

Understanding Databases

A database is a structured collection of data. It functions similarly to an Excel spreadsheet but can handle much larger datasets and more complex relationships.

  • Tables are the core structure within a database, analogous to individual worksheets in Excel.

  • Columns (or fields) define the type of data stored (e.g., name, age, price).

  • Rows (or records) contain the actual data entries, one per row.

Example: A coffee shop database might have three tables:

  • Customers table (e.g., Marvel characters who buy coffee)

  • Coffees table (e.g., types of coffee offered)

  • Orders table (tracks which customer ordered which coffee)

Databases store data in these tables and relate them to each other.

Database Management Systems (DBMS)

A DBMS (Database Management System) is software that manages databases. Popular DBMS examples include:

  • Microsoft SQL Server

  • MySQL (also written as "My SQL")

  • PostgreSQL

  • Oracle

All these systems use SQL to interact with the data, though there are slight nuances between implementations. Learning core SQL concepts allows working with any of them.

Types of Databases

There are two main database types:

  • Relational databases (RDBMS): Data is organized into related tables. Relationships are formed through keys (primary keys and foreign keys). This is the most common type and the focus of these notes.

  • Non-relational databases (NoSQL): Data is unstructured or semi-structured, not organized into tables with relationships. They have specific use cases but are not covered here.

Setting Up MySQL on Linux (Ubuntu)

  1. Open a terminal.

  2. Update package repositories:

     sudo apt update
  3. Install MySQL server:

     sudo apt install mysql-server -y
  4. Verify the service is running:

     sudo systemctl status mysql

    Should show active (running).

  5. Access the MySQL command line:

     sudo mysql

    This connects to the local MySQL server (no password needed in this setup). Typically, you might use mysql -u username -p -h host -P port to specify a user, remote server, and password.

SQL Basics: CRUD Operations

CRUD stands for Create, Read, Update, Delete — the four basic operations for persistent storage.

Creating a Database

CREATE DATABASE nc_coffee;

List all databases with SHOW DATABASES;. Switch to a specific database with:

USE nc_coffee;

Check existing tables: SHOW TABLES; — initially empty.

Creating Tables

Define the table's schema (structure of columns and data types). Example: creating a coffee table:

CREATE TABLE coffee_table (
    id INT,
    name VARCHAR(255),
    region VARCHAR(255),
    roast VARCHAR(255)
);
  • INT: integer (whole number)

  • VARCHAR(n): variable-length string of up to n characters (e.g., VARCHAR(255))

  • Commas separate column definitions; no comma after the last column.

  • Semicolons terminate SQL commands.

View the table structure with DESCRIBE coffee_table;.

Inserting Data

Add a row to the table with INSERT INTO. Values must match the column order defined in the schema.

INSERT INTO coffee_table VALUES (1, 'Default Route', 'Ethiopia', 'Light');

The response confirms 1 row affected.

Querying Data: SELECT

Retrieve data from a table:

SELECT * FROM coffee_table;

The * means all columns. To select only specific columns:

SELECT name FROM coffee_table;

Filtering with WHERE

Add a WHERE clause to filter rows based on conditions:

SELECT * FROM Avengers WHERE origin = 'Earth';

Operators: =, <, >, <=, >=, <> (not equal). Combine conditions with AND, OR, NOT:

SELECT alias FROM Avengers WHERE age < 30;
SELECT * FROM Avengers WHERE origin = 'Earth' OR origin = 'Asgard';
SELECT * FROM Avengers WHERE NOT origin = 'Earth';

Updating Data

Use UPDATE to modify existing rows:

UPDATE Avengers
SET last_name = NULL
WHERE first_name = 'Groot';

Always include a WHERE clause to avoid updating all rows.

Deleting Data

Use DELETE FROM to remove rows:

DELETE FROM Avengers WHERE first_name = 'Jeff';

Again, WHERE is critical — omitting it deletes all rows.

Altering Tables

Add a new column after the table is created:

ALTER TABLE Avengers
ADD COLUMN beard BOOLEAN;
  • BOOLEAN stores true/false values (displayed as 1 for true, 0 for false).

Update the new column for specific rows:

UPDATE Avengers SET beard = TRUE WHERE first_name = 'Thor';
UPDATE Avengers SET beard = FALSE WHERE first_name = 'Groot';

Sorting Results

Order query results with ORDER BY. Use ASC for ascending (default) or DESC for descending:

SELECT * FROM Avengers ORDER BY age ASC;   -- youngest first
SELECT * FROM Avengers ORDER BY age DESC;  -- oldest first

Next Steps: Relationships and Joins

The full power of relational databases comes from linking tables. This requires:

  • Primary key: a unique identifier for each row in a table (often an id column).

  • Foreign key: a column in one table that references the primary key of another table, creating a relationship.

For example, the Orders table would reference customer_id (from Customers) and coffee_id (from Coffees). With these keys, JOIN operations can combine data from multiple tables:

  • INNER JOIN, LEFT JOIN, etc.

  • Views can also be created to present combined data conveniently.

These concepts enable complex queries such as "How many times did Spider-Man order the dark roast?" — retrieving data across related tables.


Mastering the basics covered here — creating databases and tables, inserting, selecting, updating, deleting, filtering, and altering — provides a solid foundation to explore relational database design and advanced SQL features.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent