No history yet

SQL Fundamentals

The Structure of Relational Data

Structured Query Language, or SQL, is the universal language for interacting with relational databases. Since you're already familiar with the basics of data, we'll focus on how SQL organizes and retrieves it with precision.

The core idea of a relational database is storing data in a structured way that minimizes redundancy and enforces data integrity. The structure is built on tables.

Think of a relational database as a library, and tables as the individual books. Each book has a specific topic and is organized in a predictable way.

Tables and Relationships

A table organizes data into rows and columns, much like a spreadsheet. Each column represents an attribute (like a first name or a product price), and each row represents a single record (like a specific customer or product).

To ensure each row is unique, we use a primary key. This is a column (or set of columns) containing a value that uniquely identifies each record. In the Customers table, CustomerID would be the primary key because no two customers can have the same ID.

Tables don't exist in isolation. They are linked together to form relationships. This is done using a foreign key. A foreign key is a column in one table that refers to the primary key of another table.

In this example, one customer can have many orders, but each order belongs to only one customer. The CustomerID in the Orders table links each order back to the specific customer who placed it. This relational structure is incredibly efficient, as it avoids repeating customer information for every single order they make.

Basic SQL Syntax

SQL is a declarative language. This means you describe what data you want, and the database management system (DBMS) figures out how to retrieve it. This is different from procedural languages where you must specify the exact steps to accomplish a task.

SQL statements are the commands used to communicate with the database. While SQL is technically case-insensitive for its keywords (SELECT is the same as select), the common convention is to write them in uppercase to improve readability.

-- A basic SQL statement structure
SELECT column_name1, column_name2
FROM table_name
WHERE some_condition;

Let's break down this structure:

  • SELECT: Specifies the columns you want to retrieve data from.
  • FROM: Specifies the table you are querying.
  • WHERE: Filters the records based on a specific condition. This clause is optional.

Each statement typically ends with a semicolon (;). While not always required by every database system, it's a standard practice that clearly separates one command from the next.

Finally, you can add comments to your SQL code to explain its purpose. Single-line comments start with two hyphens (--), and multi-line comments are enclosed in /* and */. Good commenting makes your code understandable to others and your future self.

Quiz Questions 1/5

What is the primary role of a foreign key in a relational database?

Quiz Questions 2/5

The statement "SQL is a declarative language" means that you...