Basic SQL Commands#

Selecting Data#

Selecting data from tables is one of the most common operations in SQL, allowing you to retrieve specific records from your database. In SQLite, the SELECT statement is used to query data from one or more tables. This section will guide you through the syntax and provide examples to help you understand how to select data effectively.

Syntax#

The basic syntax for selecting data from a table in SQLite is as follows:

SELECT column1_name, column2_name, ...
  FROM table_name
 WHERE condition
 ORDER BY column_name [ASC|DESC];
  • column1_name, column2_name, ...: The names of the columns you want to retrieve.

  • table_name: The name of the table from which to retrieve the data.

  • condition: An optional condition to filter the rows returned by the query.

  • ORDER BY column_name [ASC|DESC]: An optional clause to sort the results by one or more columns in ascending (ASC) or descending (DESC) order.

Selecting All Columns#

To select all columns from a table, use the asterisk (*) wildcard:

SELECT * FROM table_name;

Example:

To select all columns from the users table:

SELECT * FROM users;

Selecting Specific Columns#

To select specific columns from a table, list the column names separated by commas:

SELECT column1_name, column2_name, ...
FROM table_name;

Example:

To select the name and email columns from the users table:

SELECT name, email FROM users;

Using the WHERE Clause#

The WHERE clause is used to filter the rows returned by the query based on a condition:

SELECT column1_name, column2_name, ...
  FROM table_name
 WHERE condition;

Example:

To select all users with the name 'John Doe':

SELECT * FROM users WHERE name = 'John Doe';

Using the ORDER BY Clause#

The ORDER BY clause is used to sort the results of the query by one or more columns:

SELECT column1_name, column2_name, ...
  FROM table_name
 ORDER BY column_name [ASC|DESC];

Example:

To select all users and order them by their name in ascending order:

SELECT * FROM users ORDER BY name ASC;

Using Aggregate Functions#

SQLite supports various aggregate functions such as COUNT, SUM, AVG, MIN, and MAX to perform calculations on data:

SELECT aggregate_function(column_name)
  FROM table_name
 WHERE condition;

Example:

To count the number of users in the users table:

SELECT COUNT(*) FROM users;

To find the average price of products in the products table:

SELECT AVG(UnitPrice) FROM products;

Grouping Data with GROUP BY#

The GROUP BY clause is used to group rows that have the same values in specified columns into summary rows:

SELECT column1_name, aggregate_function(column2_name)
  FROM table_name
 GROUP BY column1_name;

Example:

To find the number of orders placed by each customer:

SELECT CustomerID, COUNT(*) FROM orders GROUP BY CustomerID;

Filtering Groups with HAVING#

The HAVING clause is used to filter groups based on a condition:

SELECT column1_name, aggregate_function(column2_name)
  FROM table_name
 GROUP BY column1_name
HAVING condition;

Example:

To find customers who have placed more than 5 orders:

SELECT CustomerID, COUNT(*) FROM orders
 GROUP BY CustomerID
HAVING COUNT(*) > 5;

Joining Tables#

To retrieve data from multiple tables, you can use various types of joins (e.g., INNER JOIN, LEFT JOIN):

SELECT table1.column1_name, table2.column2_name, ...
  FROM table1
  JOIN table2
       ON table1.common_column = table2.common_column;

Example: To select order details along with product names:

SELECT od.OrderID, p.ProductName, od.Quantity, od.UnitPrice
  FROM "Order Details" od
  JOIN Products p ON od.ProductID = p.ProductID;

Using Subqueries#

A subquery is a query nested within another query. Subqueries can be used in various parts of the SQL statement:

SELECT column1_name
  FROM table_name
 WHERE column2_name = (SELECT column_name FROM table_name WHERE condition);

Example:

To select all users who placed an order with a total amount greater than $1000:

SELECT * FROM users
WHERE id IN (SELECT CustomerID FROM orders WHERE total_amount > 1000);

Example Workflow#

To illustrate a typical workflow in selecting data, let's go through a simple example:

  • Create a Database:
sqlite3 example.db
  • Create Tables:
CREATE TABLE users (
    id INTEGER PRIMARY KEY,
    name TEXT NOT NULL,
    email TEXT UNIQUE NOT NULL,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE orders (
    OrderID INTEGER PRIMARY KEY,
    CustomerID INTEGER,
    OrderDate DATETIME,
    TotalAmount REAL,
    FOREIGN KEY (CustomerID) REFERENCES users(id)
);
  • Insert Data:
INSERT INTO users (name, email) VALUES ('John Doe', 'john.doe@example.com');
INSERT INTO users (name, email) VALUES ('Alice Johnson', 'alice.johnson@example.com');

INSERT INTO orders (CustomerID, OrderDate, TotalAmount) VALUES (1, '2024-01-01', 150.00);
INSERT INTO orders (CustomerID, OrderDate, TotalAmount) VALUES (1, '2024-01-02', 200.00);
INSERT INTO orders (CustomerID, OrderDate, TotalAmount) VALUES (2, '2024-01-03', 300.00);
  • Select Data:
SELECT * FROM users;
SELECT name, email FROM users;
SELECT * FROM orders WHERE TotalAmount > 150;
SELECT * FROM users ORDER BY name ASC;
SELECT COUNT(*) FROM orders;
SELECT CustomerID, COUNT(*) FROM orders GROUP BY CustomerID;
SELECT CustomerID, COUNT(*) FROM orders GROUP BY CustomerID HAVING COUNT(*) > 1;
SELECT o.OrderID, u.name, o.TotalAmount
FROM orders o
JOIN users u ON o.CustomerID = u.id;

By mastering the SELECT statement and its various clauses, you can effectively retrieve and analyze data stored in your SQLite database. Selecting data is a fundamental operation that enables you to extract meaningful insights and perform data analysis.