SQL Basics For Beginners
SQL Basics For Beginners
SQL is a standard language for storing, manipulating and retrieving data in databases.
Our SQL tutorial will teach you how to use SQL in: MySQL, SQL Server, MS Access, Oracle, Sybase, Informix,
Postgres, and other database systems.
SQL Tutorial
SQL is a standard language for storing, manipulating and retrieving data in databases.
Our SQL tutorial will teach you how to use SQL in: MySQL, SQL Server, MS Access, Oracle,
Sybase, Informix, Postgres, and other database systems.
Example
SELECT * FROM Customers;
Introduction to SQL
What is SQL?
SQL stands for Structured Query Language
SQL lets you access and manipulate databases
SQL became a standard of the American National Standards Institute (ANSI) in 1986, and of the
International Organization for Standardization (ISO) in 1987
However, to be compliant with the ANSI standard, they all support at least the major commands
(such as SELECT, UPDATE, DELETE, INSERT, WHERE) in a similar manner.
RDBMS
RDBMS stands for Relational Database Management System.
RDBMS is the basis for SQL, and for all modern database systems such as MS SQL Server,
IBM DB2, Oracle, MySQL, and Microsoft Access.
The data in RDBMS is stored in database objects called tables. A table is a collection of related
data entries and it consists of columns and rows.
Every table is broken up into smaller entities called fields. The fields in the Customers table
consist of CustomerID, CustomerName, ContactName, Address, City, PostalCode and Country.
A field is a column in a table that is designed to maintain specific information about every record
in the table.
A record, also called a row, is each individual entry that exists in a table. For example, there are
91 records in the above Customers table. A record is a horizontal entity in a table.
A column is a vertical entity in a table that contains all information associated with a specific
field in a table.
SELECT Syntax
WHERE Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Example
SELECT * FROM Customers
WHERE Country='Mexico';
Operators in The WHERE Clause
The following operators can be used in the WHERE clause:
Operator Description
= Equal
<> Not equal. Note: In some versions of SQL this operator may be written
as !=
The AND and OR operators are used to filter records based on more than one
condition:
● The AND operator displays a record if all the conditions separated by AND
are TRUE.
● The OR operator displays a record if any of the conditions separated by OR
is TRUE.
OR Syntax
Example
SELECT * FROM Customers
NOT Syntax
Example
SELECT * FROM Customers
Example
SELECT CustomerName, ContactName, Address
FROM Customers
We will have to use the IS NULL and IS NOT NULL operators instead.
Example
SELECT CustomerName, ContactName, Address
FROM Customers
UPDATE Table
The following SQL statement updates the first customer (CustomerID = 1) with
a new contact person and a new city.
Example
UPDATE Customers
WHERE CustomerID = 1;
Example
UPDATE Customers
SET ContactName='Juan'
WHERE Country='Mexico';
There are two wildcards often used in conjunction with the LIKE operator:
Example
IN Operator Examples
The following SQL statement selects all customers that are located in
"Germany", "France" or "UK":
Example
Example
SELECT * FROM Customers
The BETWEEN operator is inclusive: begin and end values are included.
BETWEEN Example
The following SQL statement selects all products with a price between 10 and
20:
Example
Example
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
● (INNER) JOIN: Returns records that have matching values in both tables
● LEFT (OUTER) JOIN: Returns all records from the left table, and the
matched records from the right table
● RIGHT (OUTER) JOIN: Returns all records from the right table, and the
matched records from the left table
● FULL (OUTER) JOIN: Returns all records when there is a match in either
left or right table
SQL INNER JOIN Keyword
The INNER JOIN keyword selects records that have matching values in both
tables.
Example
SELECT Orders.OrderID, Customers.CustomerName
FROM Orders
Example
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
ORDER BY Customers.CustomerName;
Example
SELECT Orders.OrderID, Employees.LastName,
Employees.FirstName
FROM Orders
ORDER BY Orders.OrderID;
Tip: FULL OUTER JOIN and FULL JOIN are the same.
Note: FULL OUTER JOIN can potentially return very large result-sets!
FROM Customers
ORDER BY Customers.CustomerName;
The following SQL statement lists the number of customers in each country,
sorted high to low:
Example
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
Example
FROM Customers
GROUP BY Country
Example
SELECT Employees.LastName, COUNT(Orders.OrderID) AS NumberOfOrders
FROM (Orders
GROUP BY LastName