Learn fundamental SQL concepts that apply to all database systems
SQL (Structured Query Language) is a standard language for accessing and manipulating databases. It allows you to perform various operations like retrieving data, inserting records, updating records, and deleting records.
Key Concepts:
The SELECT statement is used to select data from a database.
SELECT column1, column2, ...
FROM table_name;
Example:
SELECT FirstName, LastName
FROM Employees;
The WHERE clause is used to filter records.
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Example:
SELECT *
FROM Customers
WHERE Country='Mexico';
The INSERT INTO statement is used to insert new records in a table.
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
Example:
INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway');
The UPDATE statement is used to modify the existing records in a table.
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Example:
UPDATE Customers
SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1;
The DELETE statement is used to delete existing records in a table.
DELETE FROM table_name WHERE condition;
Example:
DELETE FROM Customers WHERE CustomerName='Alfreds Futterkiste';
SELECT COUNT(*), AVG(Price)
FROM Products;
SELECT ProductName, UPPER(Category)
FROM Products;
JOINs are used to combine rows from two or more tables, based on a related column between them.
Returns records that have matching values in both tables.
SELECT Orders.OrderID, Customers.CustomerName
FROM Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;
Returns all records from the left table, and the matched records from the right table.
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID
ORDER BY Customers.CustomerName;
Returns all records from the right table, and the matched records from the left table.
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
RIGHT JOIN Orders ON Customers.CustomerID = Orders.CustomerID
ORDER BY Customers.CustomerName;
Returns all records when there is a match in either left or right table records.
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
FULL OUTER JOIN Orders ON Customers.CustomerID = Orders.CustomerID
ORDER BY Customers.CustomerName;