Friday, 12 June 2015

SQL Basics

What is SQL?

SQL stands for Structured Query Language
SQL lets you access and manipulate databases
SQL is an ANSI (American National Standards Institute) standard



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
------------------------------------------------------------------

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.


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



SQL SELECT Statement

The SELECT statement is used to select data from a database.

The result is stored in a result table, called the result-set.

SQL SELECT Syntax

SELECT column_name,column_name
FROM table_name;
and

SELECT * FROM table_name;

Eg: SELECT CustomerName,City FROM Customers;
Eg:SELECT * FROM Customers;


The SQL SELECT DISTINCT Statement


In a table, a column may contain many duplicate values; and sometimes you only want to list the different (distinct) values.

The DISTINCT keyword can be used to return only distinct (different) values.

SQL SELECT DISTINCT Syntax

SELECT DISTINCT column_name,column_name
FROM table_name;

Eg: SELECT DISTINCT City FROM Customers;

The SQL WHERE Clause

The WHERE clause is used to extract only those records that fulfill a specified criterion.

SQL WHERE Syntax

SELECT column_name,column_name
FROM table_name
WHERE column_name operator value;

Eg: SELECT * FROM Customers
WHERE CustomerID=1;

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 !=
>    Greater than
<    Less than
>=    Greater than or equal
<=    Less than or equal
BETWEEN    Between an inclusive range
LIKE    Search for a pattern
IN    To specify multiple possible values for a column



The SQL AND & OR Operators


The AND operator displays a record if both the first condition AND the second condition are true.

The OR operator displays a record if either the first condition OR the second condition is true.

Eg: for AND operator
SELECT * FROM Customers
WHERE Country='Germany'
AND City='Berlin';

Eg: for OR operator
SELECT * FROM Customers
WHERE City='Berlin'
OR City='München';

Eg: for both AND and OR
SELECT * FROM Customers
WHERE Country='Germany'
AND (City='Berlin' OR City='München');



The SQL ORDER BY Keyword

The ORDER BY keyword is used to sort the result-set by one or more columns.

The ORDER BY keyword sorts the records in ascending order by default.
To sort the records in a descending order, you can use the DESC keyword.

SQL ORDER BY Syntax

SELECT column_name,column_name
FROM table_name
ORDER BY column_name,column_name ASC|DESC;

Eg: SELECT * FROM Customers
ORDER BY Country,CustomerName;

The SQL INSERT INTO Statement

The INSERT INTO statement is used to insert new records in a table.


SQL INSERT INTO Syntax

It is possible to write the INSERT INTO statement in two forms.

The first form does not specify the column names where the data will be inserted, only their values:

INSERT INTO table_name
VALUES (value1,value2,value3,...);
The second form specifies both the column names and the values to be inserted:

INSERT INTO table_name (column1,column2,column3,...)
VALUES (value1,value2,value3,...);

Eg: INSERT INTO Customers
VALUES ('Cardinal','Tom B. Erichsen','Skagen 21','Stavanger','4006','Norway');
or this SQL statement (including column names):

Eg :INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
VALUES ('Cardinal','Tom B. Erichsen','Skagen 21','Stavanger','4006','Norway');

Eg: INSERT INTO Customers (CustomerName, City, Country)
VALUES ('Cardinal', 'Stavanger', 'Norway');

**he CustomerID column is an AutoNumber field and is automatically updated with a unique number
for each record in the table.

AutoNumber is a type of data used in Microsoft Access tables to generate an automatically incremented
numeric counter. The default AutoNumber type has a start value of 1 and an increment of1.

The SQL UPDATE Statement


The UPDATE statement is used to update existing records in a table.

SQL UPDATE Syntax

UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;

Eg: UPDATE Customers
SET ContactName='Alfred Schmidt', City='Hamburg'
WHERE CustomerName='Alfreds Futterkiste';


The SQL DELETE Statement


The DELETE statement is used to delete rows in a table.

SQL DELETE Syntax

DELETE FROM table_name
WHERE some_column=some_value;

Eg: DELETE FROM Customers
WHERE CustomerName='Alfreds Futterkiste' AND ContactName='Maria Anders';

Delete All Data
It is possible to delete all rows in a table without deleting the table.
This means that the table structure, attributes, and indexes will be intact:

DELETE FROM table_name;

or

DELETE * FROM table_name;
------------------------------------------------------------------

The SQL SELECT TOP Clause
The SELECT TOP clause is used to specify the number of records to return.

The SELECT TOP clause can be very useful on large tables with thousands of records.
Returning a large number of records can impact on performance.

Note: Not all database systems support the SELECT TOP clause.

SQL Server / MS Access Syntax

SELECT TOP number|percent column_name(s)
FROM table_name;

SQL SELECT TOP Equivalent in MySQL and Oracle
MySQL Syntax

SELECT column_name(s)
FROM table_name
LIMIT number;
Example

SELECT *
FROM Persons
LIMIT 5;
Oracle Syntax

SELECT column_name(s)
FROM table_name
WHERE ROWNUM <= number;
Example

SELECT *
FROM Persons
WHERE ROWNUM <=5;

Eg: SELECT TOP 2 * FROM Customers;
Eg: SELECT TOP 50 PERCENT * FROM Customers;

------------------------------------------------------------------

The SQL LIKE Operator
The LIKE operator is used to search for a specified pattern in a column.
 The "%" sign is used to define wildcards (missing letters) both before and after the pattern.

SQL LIKE Syntax

SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern;

Eg: SELECT * FROM Customers
WHERE City LIKE 's%';--------selects all customers with a City starting with the letter "s"

Eg : SELECT * FROM Customers
WHERE City LIKE '%s';--------selects all customers with a City ending with the letter "s"

Eg: SELECT * FROM Customers
WHERE Country LIKE '%land%';-------selects all customers with a Country containing the pattern "land"

Eg: SELECT * FROM Customers
WHERE Country NOT LIKE '%land%';---selects all customers with a Country NOT containing the pattern "land"

------------------------------------------------------------------
SQL Wildcards
SQL wildcards is used to substitute one or more characters when searching for data in a database.

Note: SQL wildcards must be used with the SQL LIKE operator!

With SQL, the following wildcards can be used:

%--A substitute for zero or more characters
_--A substitute for exactly one character
[charlist]----Any single character in charlist
[^charlist]
or

[!charlist]----Any single character NOT in charlist

Eg: SELECT * FROM Customers
WHERE City LIKE 'ber%';

Eg: SELECT * FROM Customers
WHERE City LIKE '%es%';

Eg: SELECT * FROM Customers
WHERE City LIKE '_erlin';---selects customer in the city that starts
 with any character, followed by "erlin" from the "Customers" table

Eg; SELECT * FROM Customers
WHERE City LIKE 'L_n_on';-----selects customers in the city that starts with a "L", followed by any character,
followed by "n", followed by any character, followed by "on", from the "Customers" table

Eg: SELECT * FROM Customers
WHERE City LIKE '[bsp]%';--------selects customers in cities starting with "b", "s", or "p" from the "Customers" table

Eg: SELECT * FROM Customers
WHERE City LIKE '[!bsp]%';------selects companies in cities NOT starting with "b" or "s" or "p" from the "Customers
------------------------------------------------------------------

he IN Operator
The IN operator allows you to specify multiple values in a WHERE clause.

SQL IN Syntax

SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1,value2,...)