Introduction to SQL

Welcome to your first lesson in SQL! In this introduction, we'll cover what SQL is, why it's essential for working with data, and how to write your first queries.

What is SQL?

SQL (Structured Query Language) is a powerful language designed for managing and manipulating data stored in relational databases. It's been the standard for database interaction for over 40 years and is used by millions of applications worldwide.

Key Features

Simple and Declarative

SQL lets you describe what data you want, not how to get it:

-- Get all users from the database
SELECT * FROM users;

Powerful Data Manipulation

SQL provides commands for creating, reading, updating, and deleting data:

-- Create a new table
CREATE TABLE products (
    id INTEGER PRIMARY KEY,
    name TEXT NOT NULL,
    price DECIMAL(10, 2)
);

-- Insert data
INSERT INTO products (id, name, price) VALUES (1, 'Laptop', 999.99);

-- Query the data
SELECT * FROM products;

Universal Language

SQL works with most database systems including PostgreSQL, MySQL, SQLite, SQL Server, and Oracle. While each has slight variations, the core SQL syntax remains consistent.

Why Learn SQL?

  1. Data Access: Query and analyze data from any relational database
  2. Career Value: SQL is one of the most in-demand skills in tech
  3. Versatility: Used in web development, data science, analytics, and more
  4. Simplicity: Easy to learn with powerful capabilities

Your First Query

The simplest SQL query selects data without even needing a table:

-- Display a simple message
SELECT 'Hello, SQL!' AS message;

-- Perform calculations
SELECT 2 + 2 AS result;

-- Work with multiple columns
SELECT
    'SQL' AS language,
    2025 AS year,
    'Awesome' AS description;

Try these exercises:

  1. Change the message to include your name
  2. Try different mathematical operations (*, /, -)
  3. Add more columns to the SELECT statement
  4. Change the column aliases (the names after AS)

Interactive Playground

Practice what you've learned! Edit and run the SQL code below to experiment with your first database.

-- Create a simple table
CREATE TABLE students (
    id INTEGER PRIMARY KEY,
    name TEXT NOT NULL,
    age INTEGER,
    grade TEXT
);

-- Add some students
INSERT INTO students (id, name, age, grade) VALUES (1, 'Alice', 20, 'A');
INSERT INTO students (id, name, age, grade) VALUES (2, 'Bob', 22, 'B');
INSERT INTO students (id, name, age, grade) VALUES (3, 'Charlie', 21, 'A');

-- Query all students
SELECT * FROM students;

Try these exercises:

  1. Add yourself as a new student to the table
  2. Change the ages or grades of existing students
  3. Add a new column like "major" to the table schema
  4. Query only specific columns instead of using *

Pro Tip: SQL is not case-sensitive, but it's a common convention to write SQL keywords in UPPERCASE and table/column names in lowercase. This makes your queries easier to read!

Introduction to SQL | LearningSQL.org