Basic Queries
In the world of database management, tables and queries go hand in hand. Queries are a fundamental component of any database, as they allow you to retrieve and manipulate data in meaningful ways.
In this chapter, we will explore the basics of SQL queries and how they are used to extract data from tables. To demonstrate this, we will use two example tables, the orders and customers tables.
These tables will be linked together using a foreign key to show how queries can retrieve data from multiple tables at once. Understanding queries and the relationship between tables is essential for effective database management, as it enables developers to extract valuable insights and make informed decisions based on data.
This is the example table called customers:
CREATE TABLE customers (
id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
email VARCHAR(100),
address VARCHAR(100),
city VARCHAR(50),
state VARCHAR(50),
zip_code VARCHAR(20)
);
This table has columns for a customer's ID, first name, last name, email address, street address, city, state, and zip code. The id column is the primary key for the table, which means that each row in the table is uniquely identified by its value in the id column.
This is the example of an orders table:
CREATE TABLE orders (
order_id INT PRIMARY KEY,
customer_id INT,
order_date DATE,
total_price DECIMAL(10,2)
);
This table has four columns: order_id, customer_id, order_date, and total_price. The order_id column is the primary key of the table, which means that each row has a unique value in that column.
The customer_id column is a foreign key that references the customer_id column in the customers table.
This establishes a relationship between the two tables.
The orders table contains information about each order placed by a customer. The customer_id column is used to link each order to a specific customer in the customers table.
The order_date column contains the date that the order was placed, and the total_price column contains the total price of the order. By joining the orders table with the customers table on the customer_id column, we can retrieve information about both the customer and their order in a single query.
Overview of Different SELECT Commands and Syntax
The SELECT statement has a variety of options for retrieving and manipulating data. Here are some examples:
-
The
This statement retrieves all columns and rows from theWHEREclause is used to filter data based on a specified condition:customerstable where the city isLondon. -
The
This statement retrieves all columns and rows from theORDER BYclause is used to sort data by one or more columns:customerstable, sorted by thelast_namecolumn. -
The
This statement retrieves theGROUP BYclause is used to group data by one or more columns:citycolumn and a count of how many times eachcityappears in thecustomerstable. -
The
This statement retrieves all columns and rows from both theJOINcommand is used to combine data from two or more tables:customersandorderstables where thecustomer_idcolumn matches in both tables.