CONTENTS
SQL Intro
SQL Syntax
SQL Select
SQL Select Distinct
SQL Where
SQL Order By
SQL And
SQL Or
1
SQL
Structured Query Language
For storing, manipulating and retrieving data in dababases.
Available databases: MySQL, SQL server, MS access, Oracle, Sybase, Informix,
Postgres etc.,
What Can SQL do?
SQL can execute queries against a database
SQL can retrieve data from a database
SQL can insert records in a database
SQL can update records in a database
SQL can delete records from a database
SQL can create new databases
SQL can create new tables in a database
SQL can create stored procedures in a database
SQL can create views in a database
SQL can set permissions on tables, procedures, and views
To build a web site that shows data from a database, you will need:
An RDBMS database program (i.e. MS Access, SQL Server, MySQL)
To use a server-side scripting language, like PHP or ASP
To use SQL to get the data you want
To use HTML / CSS to style the page
RDBMS
RDBMS stands for Relational Database Management System.
RDBMS is the basis for SQL, and for all modern database systems.
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.
Some of The Most Important SQL Commands
SELECT - extracts data from a database
UPDATE - updates data in a database
DELETE - deletes data from a database
INSERT INTO - inserts new data into a database
CREATE DATABASE - creates a new database
ALTER DATABASE - modifies a database
CREATE TABLE - creates a new table
ALTER TABLE - modifies a table
DROP TABLE - deletes a table
CREATE INDEX - creates an index (search key)
DROP INDEX - deletes an index
2
SELECT
SELECT column1, column2, ...
FROM table_name;
DEMO DATABASE: Orders Table
To select all the records in this database,
SELECT * FROM Orders;
To select one particular record (column),
SELECT EmployeeID FROM Orders;
3
SELECT DISTINCT
A table might have duplicate values. So we can use distinct keyword to display accurate
values arranged properly.
Here is the different between SELECT and SELECT DISTNCT:
Here, as we can see, there is difference in the number of records as many values are repeated
such as Germany.
And also in the SELECT DISTINCT, we can see that the records are arranged in alphabetical
order.
So the best practice is to use SELECT DISTINCT instead of SELECT
4
WHERE
The WHERE clause is used to filter the records given a specific condition.
SELECT column1, column2, ...
FROM table_name
WHERE condition;
So here we have given the condition in the format
City = ‘London’;
The single quotes is for the datatype varchar. For numbers we could just write,
CustomerID = 1;
5
WHERE OPERATORS
= equals
> greater than
< less than
>= greater than or equal to
<= less than or equal to
<> not equal to (!=)
BETWEEN between a certain range
LIKE search for a pattern
IN to specify multiple possible values
NOTE : AND operator is used for BETWEEN operator as it is a range.
6
ORDER BY
To sort the result set in ascending or descending order.
SELECT * FROM Products
ORDER BY Price;
7
ASC & DESC
The ORDER BY keyword sorts the records in ascending order by default. To sort the records
in descending order, use the DESC keyword.
SELECT * FROM Products
ORDER BY Price DESC;
SELECT * FROM Customers
ORDER BY Country ASC, CustomerName DESC;
8
AND
The WHERE clause can contain one or more AND operators.
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;
AND vs OR
The AND operator displays a record if all the conditions are TRUE.
The OR operator displays a record if any of the conditions are TRUE.
We can combine both AND and OR in a same query by inserting them into brackets.
Thara
9
10