-->

Section 2 - SQL Query

What is SQL Query

What is SQL query

An SQL query is a specific request or command written in Structured Query Language (SQL) that you send to a database management system (DBMS) to perform operations on data stored in a relational database.

Key Characteristics of SQL Queries

  1. Instructions to the database: Tells the DBMS what action to perform
  2. Returns a result set: Most queries return data in table format (rows and columns)
  3. Can be simple or complex: From basic lookups to multi-table operations with calculations

Common Types of SQL Queries

1. Data Retrieval Queries (SELECT)

SELECT product_name, price 
FROM products 
WHERE category = 'Electronics' 
ORDER BY price DESC;
  

2. Data Manipulation Queries

INSERT: Adds new records

INSERT INTO customers(name, email) 
VALUES ('John Doe','[email protected]');
  

UPDATE: Modifies existing records

UPDATE employees 
SET salary = salary * 1.1 
WHERE department = 'Engineering';
  

DELETE: Removes records

DELETE FROM orders 
WHERE status = 'cancelled';
  

3. Schema Definition Queries

CREATE TABLE students (
  student_id INT PRIMARY KEY,
  name VARCHAR(50),
  enrollment_date DATE
);
  

4. Join Queries (Combine data from multiple tables)

SELECT orders.order_id,
       customers.name,
       orders.order_date
FROM orders
JOIN customers 
ON orders.customer_id = customers.customer_id;
  

Query Structure Components

  1. Clauses: Keywords like SELECT, FROM, WHERE, GROUP BY, etc.
  2. Expressions: Produce scalar values or tables
  3. Predicates: Conditions that evaluate to TRUE, FALSE, or UNKNOWN
  4. Parameters: Variables that can change query behaviour

Note: Throughout the tutorials, I will be using the Sakila Sample Database. Please refer to https://dev.mysql.com/doc/sakila/en/sakila-installation.html