0% found this document useful (0 votes)
18 views

SQL Notes

Uploaded by

khwezi sokhela
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

SQL Notes

Uploaded by

khwezi sokhela
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

Basic Structure of an SQL Query


sql
Copy code
SELECT column1, column2, ...
FROM table_name
WHERE condition;

• SELECT: Specifies the columns to retrieve.


• FROM: Specifies the table to retrieve the data from.
• WHERE: Filters records based on a specified condition (optional).

2. Example Queries

a) Selecting All Data from a Table

sql
Copy code
SELECT *
FROM Customers;

• Retrieves all columns from the Customers table.

b) Selecting Specific Columns

sql
Copy code
SELECT FirstName, LastName, Email
FROM Customers;

• Retrieves the FirstName, LastName, and Email columns from the Customers table.

c) Using a WHERE Clause to Filter Results

sql
Copy code
SELECT *
FROM Orders
WHERE OrderDate > '2023-01-01';

• Retrieves all orders placed after January 1, 2023.

d) Inserting Data

sql
Copy code
INSERT INTO Customers (FirstName, LastName, Email)
VALUES ('John', 'Doe', 'john.doe@example.com');

• Inserts a new row into the Customers table.

e) Updating Data
sql
Copy code
UPDATE Customers
SET Email = 'john.doe2024@example.com'
WHERE CustomerID = 1;

• Updates the Email field for the customer with CustomerID 1.

f) Deleting Data

sql
Copy code
DELETE FROM Customers
WHERE CustomerID = 1;

• Deletes the customer with CustomerID 1.

g) Creating a Table

sql
Copy code
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
HireDate DATE
);

• Creates an Employees table with columns for EmployeeID, FirstName, LastName,


and HireDate.

h) Joining Tables

sql
Copy code
SELECT Customers.FirstName, Customers.LastName, Orders.OrderDate
FROM Customers
JOIN Orders ON Customers.CustomerID = Orders.CustomerID;

• Retrieves customer names along with their order dates by joining Customers and
Orders tables.

3. Key SQL Keywords and Functions

• SELECT: Retrieve data.


• INSERT INTO: Add new data.
• UPDATE: Modify existing data.
• DELETE: Remove data.
• JOIN: Combine rows from multiple tables.
• WHERE: Filter data.
• ORDER BY: Sort results.
• GROUP BY: Group rows that have the same values in specified columns.
• COUNT(), SUM(), AVG(): Aggregate functions for counting, summing, or averaging.

You might also like