Monday 7 November 2016

SQL Queries with examples

Create Table:
The CREATE TABLE statement is used to create a table in a database.

SQL CREATE TABLE Syntax


CREATE TABLE table_name
(
column_name1 data_type,
column_name2 data_type,
column_name3 data_type,
....
)

E.g.:
CREATE TABLE Persons
(
P_Id int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)

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(s)
FROM table_name
and
SELECT * FROM table_name

An SQL SELECT Example: The "Persons" table:

P_Id
LastName
FirstName
Address
City
1
Hansen
Ola
Timoteivn 10
Sandnes
2
Svendson
Tove
Borgvn 23
Sandnes
3
Pettersen
Kari
Storgt 20
Stavanger

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

SQL WHERE Syntax:


SELECT column_name(s)
FROM table_name
WHERE column_name operator value

SELECT * FROM Persons
WHERE City
='Sandnes'

The result-set will look like this:

P_Id
LastName
FirstName
Address
City
1
Hansen
Ola
Timoteivn 10
Sandnes
2
Svendson
Tove
Borgvn 23
Sandnes

Insert into Statement
The INSERT INTO statement is used to insert a new row in a table. SQL INSERT INTO Syntax: It is possible to write the INSERT INTO statement in two forms. The first form doesn't specify the column names where the data will be inserted, only their values. 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,...)
Now we want to insert a new row in the "Persons" table. We use the following SQL statement:

INSERT INTO Persons
VALUES (4,'Nilsen', 'Johan', 'Bakken 2', 'Stavanger')
The "Persons" table will now look like this:

P_Id
LastName
FirstName
Address
City
1
Hansen
Ola
Timoteivn 10
Sandnes
2
Svendson
Tove
Borgvn 23
Sandnes
3
Pettersen
Kari
Storgt 20
Stavanger
4
Nilsen
Johan
Bakken 2
Stavanger
Update Statement:
The UPDATE statement is used to update existing records in a table.

SQL UPDATE Syntax:

UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value
UPDATE Persons
SET Address='Nissestien 67', City='Sandnes'
WHERE LastName='Tjessem' AND FirstName='Jakob'

The "Persons" table will now look like this:


P_Id
LastName
FirstName
Address
City
1
Hansen
Ola
Timoteivn 10
Sandnes
2
Svendson
Tove
Borgvn 23
Sandnes
3
Pettersen
Kari
Storgt 20
Stavanger
4
Nilsen
Johan
Bakken 2
Stavanger
5
Tjessem
Jakob
Nissestien 67
Sandnes

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

DELETE FROM Persons
WHERE LastName='Tjessem' AND FirstName='Jakob'
The "Persons" table will now look like this:

P_Id
LastName
FirstName
Address
City
1
Hansen
Ola
Timoteivn 10
Sandnes
2
Svendson
Tove
Borgvn 23
Sandnes
3
Pettersen
Kari
Storgt 20
Stavanger
4
Nilsen
Johan
Bakken 2
Stavanger

Delete All Rows: 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
Distinct Statement:
In a table, some of the columns may contain duplicate values. This is not a problem, however, sometimes you will want to list only the different (distinct) values in a table.
The DISTINCT keyword can be used to return only distinct (different) values.

SQL SELECT DISTINCT Syntax:

SELECT DISTINCT column_name(s)
FROM table_name

SELECT DISTINCT Example: The "Persons" table:

P_Id
LastName
FirstName
Address
City
1
Hansen
Ola
Timoteivn 10
Sandnes
2
Svendson
Tove
Borgvn 23
Sandnes
3
Pettersen
Kari
Storgt 20
Stavanger

Now we want to select only the distinct values from the column named "City" from the table above. We use the following SELECT statement:

SELECT DISTINCT City FROM Persons
The result-set will look like this:

City
Sandnes
Stavanger 
And and OR Operators:
The AND operator displays a record if both the first condition and the second condition is true. The OR operator displays a record if either the first condition or the second condition is true.

SELECT * FROM Persons
WHERE FirstName='Tove'
AND LastName='Svendson'

The result-set will look like this:


P_Id
LastName
FirstName
Address
City
2
Svendson
Tove
Borgvn 23
Sandnes

OR Operator Example: Now we want to select only the persons with the first name equal to "Tove" OR the first name equal to "Ola": We use the following SELECT statement:

SELECT * FROM Persons
WHERE FirstName='Tove'
OR FirstName='Ola'

The result-set will look like this:

P_Id
LastName
FirstName
Address
City
1
Hansen
Ola
Timoteivn 10
Sandnes
2
Svendson
Tove
Borgvn 23
Sandnes

Combining AND & OR: You can also combine AND and OR (use parenthesis to form complex expressions). Now we want to select only the persons with the last name equal to "Svendson" AND the first name equal to "Tove" OR to "Ola": We use the following SELECT statement:

SELECT * FROM Persons WHERE
LastName='Svendson'
AND (FirstName='Tove' OR FirstName='Ola')

The result-set will look like this:

P_Id
LastName
FirstName
Address
City
2
Svendson
Tove
Borgvn 23
Sandnes
Order By:
The ORDER BY keyword is used to sort the result-set by a specified column. The ORDER BY keyword sort the records in ascending order by default. If you want to sort the records in a descending order, you can use the DESC keyword.

SQL ORDER BY Syntax:

SELECT column_name(s)
FROM table_name
ORDER BY column_name(s) ASC|DESC
SELECT * FROM Persons
ORDER BY LastName

The result-set will look like this:

P_Id
LastName
FirstName
Address
City
1
Hansen
Ola
Timoteivn 10
Sandnes
4
Nilsen
Tom
Vingvn 23
Stavanger
3
Pettersen
Kari
Storgt 20
Stavanger
2
Svendson
Tove
Borgvn 23
Sandnes

ORDER BY DESC Example: Now we want to select all the persons from the table above, however, we want to sort the persons descending by their last name. We use the following SELECT statement:

SELECT * FROM Persons
ORDER BY LastName DESC

The result-set will look like this:

P_Id
LastName
FirstName
Address
City
2
Svendson
Tove
Borgvn 23
Sandnes
3
Pettersen
Kari
Storgt 20
Stavanger
4
Nilsen
Tom
Vingvn 23
Stavanger
1
Hansen
Ola
Timoteivn 10
Sandnes

No comments:

Post a Comment