SQL: A Comprehensive Guide

SQL (Structured Query Language) is the standard language for managing and querying relational databases. Learn how to create, manipulate, and retrieve data efficiently with SQL.

What is SQL?

SQL is a domain-specific language used to interact with relational databases. It allows users to define database structures, insert and update data, and query information using standardized syntax.

How SQL Works

SQL works by sending queries to a database management system, which processes the query and returns results. Here’s a simplified process:

  1. Query Submission: The user writes an SQL query (e.g., SELECT * FROM users;) and submits it.
  2. Query Parsing: The DBMS parses the query to ensure correct syntax and semantics.
  3. Query Optimization: The DBMS creates an execution plan to retrieve data efficiently.
  4. Data Retrieval: The database engine accesses the data, applying filters, joins, or aggregations.
  5. Result Delivery: The results are returned to the client for display or further processing.

SQL Basic Syntax

SQL syntax is declarative, meaning you specify what you want, not how to get it. Below are the core SQL commands with examples.

SELECT

Retrieves data from one or more tables.

SELECT name, age FROM users WHERE age > 18;

INSERT

Adds new records to a table.

INSERT INTO users (name, age) VALUES ('John', 25);

UPDATE

Modifies existing records.

UPDATE users SET age = 26 WHERE name = 'John';

DELETE

Removes records from a table.

DELETE FROM users WHERE age < 18;

CREATE TABLE

Defines a new table structure.

CREATE TABLE users (
    id INT PRIMARY KEY,
    name VARCHAR(50),
    age INT
);

DROP TABLE

Deletes a table and its data.

DROP TABLE users;

All Types of SQL Queries

SQL queries are categorized into four main types: DDL, DML, DCL, and TCL.

Data Definition Language (DDL)

Defines and modifies database structures.

-- Create a table
CREATE TABLE products (
    id INT PRIMARY KEY,
    name VARCHAR(100),
    price DECIMAL(10, 2)
);
-- Alter table to add a column
ALTER TABLE products ADD stock INT;
-- Drop table
DROP TABLE products;

Data Manipulation Language (DML)

Manipulates data within tables.

-- Insert data
INSERT INTO products (id, name, price, stock) VALUES (1, 'Laptop', 999.99, 50);
-- Update data
UPDATE products SET price = 1099.99 WHERE id = 1;
-- Delete data
DELETE FROM products WHERE stock = 0;

Data Control Language (DCL)

Manages access and permissions.

-- Grant permissions
GRANT SELECT, INSERT ON users TO 'user1';
-- Revoke permissions
REVOKE INSERT ON users FROM 'user1';

Transaction Control Language (TCL)

Manages transactions to ensure data integrity.

-- Start transaction
BEGIN TRANSACTION;
-- Commit changes
COMMIT;
-- Rollback changes
ROLLBACK;

Core Concepts

Master these essential SQL concepts to work effectively with databases.

JOIN

Combines data from multiple tables based on a related column.

SELECT users.name, orders.order_date
FROM users
INNER JOIN orders ON users.id = orders.user_id;

GROUP BY

Groups rows with similar values for aggregate calculations.

SELECT age, COUNT(*) AS count
FROM users
GROUP BY age;

ORDER BY

Sorts query results in ascending or descending order.

SELECT name, age
FROM users
ORDER BY age DESC;

Subqueries

Nested queries to retrieve data based on intermediate results.

SELECT name
FROM users
WHERE id IN (SELECT user_id FROM orders WHERE total > 100);

Common SQL Functions

SQL provides built-in functions for data manipulation and analysis.

Aggregate Functions

Perform calculations on multiple rows.

SELECT COUNT(*) AS total_users,
       AVG(age) AS avg_age,
       SUM(salary) AS total_salary
FROM users;

String Functions

Manipulate string data.

SELECT UPPER(name) AS uppercase_name,
       CONCAT(name, ' ', age) AS name_age
FROM users;

MySQL vs. SQL

SQL is the language, while MySQL is a specific RDBMS that uses SQL.

Using SQL with HTML, CSS, JavaScript

SQL is typically used with a backend language like PHP to connect HTML/CSS/JS frontends to a database. Below is a PHP-MySQL example to display users.

PHP with MySQL

A simple web app to display users from the users table, styled with Tailwind CSS.

<!-- index.php -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Users List</title>
    <link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
</head>
<body class="bg-gray-900 p-8 text-gray-200">
    <h1 class="text-2xl font-bold mb-4">Users List</h1>
    <?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "mydb";

    try {
        $conn = new mysqli($servername, $username, $password, $dbname);
        if ($conn->connect_error) {
            throw new Exception("Connection failed: " . $conn->connect_error);
        }
        $result = $conn->query("SELECT name, age FROM users");
        if ($result->num_rows > 0) {
            echo '<table class="w-full border-collapse bg-gray-800 shadow-md rounded-lg" aria-label="Users table">';
            echo '<thead><tr class="bg-gray-700"><th class="p-4 text-left">Name</th><th class="p-4 text-left">Age</th></tr></thead><tbody>';
            while ($row = $result->fetch_assoc()) {
                echo "<tr><td class='p-4 border-t border-gray-700'>" . htmlspecialchars($row['name']) . "</td><td class='p-4 border-t border-gray-700'>" . $row['age'] . "</td></tr>";
            }
            echo '</tbody></table>';
        } else {
            echo '<div class="p-4 bg-yellow-900 text-yellow-200 rounded-lg" role="alert">No users found in the database.</div>';
        }
        $conn->close();
    } catch (Exception $e) {
        echo '<div class="p-4 bg-red-900 text-red-200 rounded-lg" role="alert">Error: ' . htmlspecialchars($e->getMessage()) . '</div>';
    }
    ?>
</body>
</html>

Detailed Setup and Usage Instructions:

  1. Install XAMPP: Download and install XAMPP from apachefriends.org. It includes Apache, MySQL, and PHP.
  2. Start Services: Open the XAMPP Control Panel and start the Apache and MySQL modules.
  3. Create Database: Access phpMyAdmin by navigating to http://localhost/phpmyadmin. Create a database named mydb (see SQL code in the next section).
  4. Save PHP File: Save the above code as index.php in the htdocs folder of your XAMPP installation (e.g., C:\xampp\htdocs).
  5. Update Credentials: Modify $servername, $username, $password, and $dbname in the PHP code if your MySQL setup uses different credentials (default: root, no password).
  6. Run the Application: Open a browser and go to http://localhost/index.php. The page should display a table of users if the database is set up correctly.
  7. Troubleshooting: If you see errors, check MySQL connection details, ensure the users table exists, and verify that Apache and MySQL are running.

Sample Users Table Creation

Below is the SQL to create and populate the users table used in the PHP example.

-- Create the database
CREATE DATABASE IF NOT EXISTS mydb;
USE mydb;

-- Create the users table
CREATE TABLE users (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(50),
    age INT
);

-- Insert random data
INSERT INTO users (name, age) VALUES
('Alice Smith', 28),
('Bob Johnson', 34),
('Charlie Brown', 19),
('Diana Lee', 45),
('Emma Davis', 22),
('Frank Wilson', 30),
('Grace Kim', 27),
('Henry Clark', 41);

Detailed Setup and Usage Instructions:

  1. Access phpMyAdmin: Open http://localhost/phpmyadmin in your browser after starting MySQL in XAMPP.
  2. Create Database: Click "New" in phpMyAdmin, enter mydb as the database name, and click "Create".
  3. Run SQL: In phpMyAdmin, select the mydb database, go to the "SQL" tab, paste the above code, and click "Go" to execute it.
  4. Verify Table: Check that the users table appears in phpMyAdmin with 8 rows of sample data.
  5. Test Queries: Use the phpMyAdmin "SQL" tab to test queries like SELECT * FROM users; to ensure data is correctly inserted.
  6. Alternative Tools: You can also use MySQL Workbench or the MySQL command-line client to run the SQL code.

Using SQL in Java

Java uses JDBC (Java Database Connectivity) to interact with SQL databases.

Java with MySQL

Retrieve and display user data from the users table.

import java.sql.*;

public class Main {
    public static void main(String[] args) {
        try {
            // Load MySQL driver
            Class.forName("com.mysql.cj.jdbc.Driver");
            // Connect to database
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "");
            // Create statement
            Statement stmt = conn.createStatement();
            // Execute query
            ResultSet rs = stmt.executeQuery("SELECT name, age FROM users");
            // Process results
            while (rs.next()) {
                System.out.println("Name: " + rs.getString("name") + ", Age: " + rs.getInt("age"));
            }
            // Close connection
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Detailed Setup and Usage Instructions:

  1. Install Java: Ensure you have the Java Development Kit (JDK) installed (e.g., OpenJDK or Oracle JDK). Download from oracle.com or use a package manager.
  2. Download MySQL Connector/J: Get the MySQL JDBC driver from dev.mysql.com. Extract the JAR file (e.g., mysql-connector-java-x.x.xx.jar).
  3. Set Up Project: Create a new Java project in an IDE like IntelliJ IDEA, Eclipse, or VS Code. Add the MySQL Connector JAR to your project’s classpath (e.g., in IntelliJ, go to File > Project Structure > Libraries).
  4. Save Code: Save the above code as Main.java in your project’s source directory.
  5. Ensure Database: Verify that the mydb database and users table exist (see previous section).
  6. Update Credentials: Modify the connection string (jdbc:mysql://localhost:3306/mydb, root, "") if your MySQL setup uses different credentials.
  7. Run the Program: Compile and run the Java program in your IDE or via the command line: javac Main.java and java -cp .;mysql-connector-java-x.x.xx.jar Main (replace ; with : on Unix-based systems).
  8. Expected Output: The program should print the names and ages of users in the console.
  9. Troubleshooting: Ensure MySQL is running, the JDBC driver is in the classpath, and the database credentials are correct. Check for exceptions in the console output.

Using SQL in Python

Python uses libraries like mysql-connector-python to interact with SQL databases.

Python with MySQL

Fetch and display user data from the users table.

import mysql.connector

# Connect to database
conn = mysql.connector.connect(
    host="localhost",
    user="root",
    password="",
    database="mydb"
)
cursor = conn.cursor()

# Execute query
cursor.execute("SELECT name, age FROM users")
rows = cursor.fetchall()

# Display results
for row in rows:
    print(f"Name: {row[0]}, Age: {row[1]}")

# Close connection
cursor.close()
conn.close()

Detailed Setup and Usage Instructions:

  1. Install Python: Ensure Python 3 is installed. Download from python.org or use a package manager.
  2. Install MySQL Connector: Run pip install mysql-connector-python in your terminal or command prompt to install the library.
  3. Ensure Database: Verify that the mydb database and users table exist (see the "Sample Users Table Creation" section).
  4. Save Code: Save the above code as main.py in a directory of your choice.
  5. Update Credentials: Modify host, user, password, and database in the code if your MySQL setup uses different credentials.
  6. Run the Script: Open a terminal, navigate to the directory containing main.py, and run python main.py.
  7. Expected Output: The script should print the names and ages of users in the terminal.
  8. Troubleshooting: Ensure MySQL is running, the mysql-connector-python library is installed, and the database credentials are correct. Check for error messages in the terminal.

Why SQL is Better

SQL offers distinct advantages over other data management approaches.

Best Practices for SQL

Guidelines for efficient and secure SQL queries:

SQL Notes & Resources

Access SQL notes, cheat sheets, and interview questions.

Revise Core Technologies

SQL integrates with other technologies. Revise these to enhance your skills.