Section 2 - 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
- Instructions to the database: Tells the DBMS what action to perform
- Returns a result set: Most queries return data in table format (rows and columns)
- 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
- Clauses: Keywords like SELECT, FROM, WHERE, GROUP BY, etc.
- Expressions: Produce scalar values or tables
- Predicates: Conditions that evaluate to TRUE, FALSE, or UNKNOWN
- 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
Post a Comment