beginner Step 1 of 15

Introduction to SQL

SQL & Databases

Introduction to SQL

SQL (Structured Query Language) is the standard language for managing and querying relational databases. Created in the 1970s at IBM, SQL has remained the dominant database language for over 50 years. Every major tech company, from startups to Fortune 500 corporations, relies on SQL databases to store and manage their data. Whether you use MySQL, PostgreSQL, SQLite, SQL Server, or Oracle, the core SQL syntax is remarkably consistent across all of them. Learning SQL is one of the highest-return skills you can develop as a developer.

Basic SQL Syntax

-- SQL is case-insensitive but convention uses UPPERCASE for keywords
-- Comments start with -- or /* */

-- Select all columns from a table
SELECT * FROM users;

-- Select specific columns
SELECT name, email, age FROM users;

-- Filter rows
SELECT name, email FROM users WHERE age > 25;

-- Count rows
SELECT COUNT(*) AS total_users FROM users;

-- Insert a row
INSERT INTO users (name, email, age) VALUES ('Alice', 'alice@example.com', 30);

-- Update rows
UPDATE users SET age = 31 WHERE name = 'Alice';

-- Delete rows
DELETE FROM users WHERE id = 5;

-- Create a table
CREATE TABLE posts (
    id INT PRIMARY KEY AUTO_INCREMENT,
    user_id INT NOT NULL,
    title VARCHAR(255) NOT NULL,
    body TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id)
);
Pro tip: Always use a WHERE clause with UPDATE and DELETE statements. Running DELETE FROM users without a WHERE clause deletes ALL rows in the table. Use SELECT with the same WHERE clause first to verify which rows will be affected before running destructive operations.

Key Takeaways

  • SQL is the standard language for relational databases, consistent across MySQL, PostgreSQL, SQLite, and others.
  • The four main operations are SELECT (read), INSERT (create), UPDATE (modify), and DELETE (remove).
  • Always use WHERE clauses with UPDATE and DELETE to avoid accidentally modifying all rows.
  • SQL keywords are case-insensitive, but the convention is to use UPPERCASE for readability.
  • Use PRIMARY KEY and FOREIGN KEY constraints to enforce data integrity.