Presentation On SQL
Presentation On SQL
ON
SQL
Syntax
Some SQL tutorials end each SQL statement with a semicolon. Is this
necessary? We are using MS Access and SQL Server 2000 and we do not
have to put a semicolon after each SQL statement, but some database
programs force you to use it.
To select ALL values from the column named "Company" we use a SELECT
statement like this:
SELECT Company FROM Orders
"Orders" table
Syntax
To select only the persons living in the city "Sandnes", we add a WHERE clause to the
SELECT statement:
Result
Using LIKE
The following SQL statement will return persons with first names that
start with an 'O':
'The following SQL statement will return persons with first names that
contain the pattern 'la':
The INSERT INTO statement is used to insert new rows into a table.
Syntax
INSERT INTO table_nameVALUES (value1, value2,....)
You can also specify the columns for which you want to insert data:
INSERT INTO table_name (column1, column2,...)VALUES (value1, value2,....)The
Update Statement
The UPDATE statement is used to modify the data in a table.
Syntax
UPDATE table_nameSET column_name = new_valueWHERE column_name =
some_valueUpdate several Columns in a Row
We want to change the address and add the name of the city:
UPDATE PersonSET Address = 'Stien 12', City = 'Stavanger'WHERE LastName =
'Rasmussen'
Example
AND & OR
AND and OR join two or more conditions in a WHERE clause.
The AND operator displays a row if ALL conditions listed are true. The OR
operator displays a row if ANY of the conditions listed are true.
Use AND to display each person with the first name equal to "Tove", and the last
name equal to "Svendson":
SELECT * FROM PersonsWHERE FirstName='Tove'AND
LastName='Svendson‘
Result:
The BETWEEN ... AND operator selects a range of data between two values. These values can be numbers,
text, or dates.
Sometimes we have to select data from two or more tables to make our result complete. We
have to perform a join.
Tables in a database can be related to each other with keys. A primary key is a column with a
unique value for each row. The purpose is to bind data together, across tables, without
repeating all of the data in every table.
In the "Employees" table below, the "Employee_ID" column is the primary key, meaning
that no two rows can have the same Employee_ID. The Employee_ID distinguishes two
persons even if they have the same name.
When you look at the example tables below, notice that:
The "Employee_ID" column is the primary key of the "Employees" table
The "Prod_ID" column is the primary key of the "Orders" table
The "Employee_ID" column in the "Orders" table is used to refer to the persons in the
Employees" table without using their names
Employees:
Orders:
Result
OR we can select data from two tables with the JOIN keyword, like this:
Example INNER JOIN
Syntax
SELECT field1, field2, field3FROM first_tableINNER JOIN second_tableON first_table.keyfield =
second_table.foreign_keyfield
Who has ordered a product, and what did they order?
SELECT Employees.Name, Orders.ProductFROM EmployeesINNER JOIN OrdersON
Employees.Employee_ID=Orders.Employee_ID
The INNER JOIN returns all rows from both tables where there is a match. If there are rows in Employees
that do not have matches in Orders, those rows will not be listed.
Syntax
SELECT field1, field2, field3FROM first_tableLEFT JOIN second_tableON
first_table.keyfield = second_table.foreign_keyfield
EXAMPLE
List all employees, and their orders - if any.
SELECT Employees.Name, Orders.ProductFROM EmployeesLEFT JOIN OrdersON
Employees.Employee_ID=Orders.Employee_ID
Syntax
SELECT field1, field2, field3FROM first_tableRIGHT JOIN second_tableON
first_table.keyfield = second_table.foreign_keyfield
Sometimes we have to select data from two or more tables to make our result complete. We
have to perform a join.
Tables in a database can be related to each other with keys. A primary key is a column with a
unique value for each row. The purpose is to bind data together, across tables, without
repeating all of the data in every table.
In the "Employees" table below, the "Employee_ID" column is the primary key, meaning
that no two rows can have the same Employee_ID. The Employee_ID distinguishes two
persons even if they have the same name.
When you look at the example tables below, notice that:
The "Employee_ID" column is the primary key of the "Employees" table
The "Prod_ID" column is the primary key of the "Orders" table
The "Employee_ID" column in the "Orders" table is used to refer to the persons in the
"Employees" table without using their names
Employees:
Orders:
We can select data from two tables by referring to two tables, like this:
Syntax
SELECT field1, field2, field3FROM
first_tableINNER JOIN second_
tableON first_table.keyfield =
second_table.foreign_keyfield
Who has ordered a product, and what did they
order?
SELECT Employees.Name,
Orders.ProductFROM EmployeesINNER
JOIN OrdersON
Employees.Employee_ID=Orders.Employee_I
D
The INNER JOIN returns all rows from both
tables where there is a match. If there are rows in
Employees that do not have matches in Orders,
those rows will not be listed.
Result
Syntax
SELECT field1, field2, field3FROM
first_tableLEFT JOIN second_tableON
first_table.keyfield =
second_table.foreign_keyfield
List all employees, and their orders - if any.
SELECT Employees.Name,
Orders.ProductFROM EmployeesLEFT JOIN
OrdersON
Employees.Employee_ID=Orders.Employee_I
D
The LEFT JOIN returns all the rows from the first
table (Employees), even if there are no matches in
the second table (Orders). If there are rows in
Employees that do not have matches in Orders,
those rows also will be listed.
Syntax
SELECT field1, field2, field3FROM
first_tableRIGHT JOIN second_tableON
first_table.keyfield =
second_table.foreign_keyfield
SELECT Employees.Name,
Orders.ProductFROM EmployeesRIGHT JOIN
OrdersON
Employees.Employee_ID=Orders.Employee_ID
To create a database:
Create a Table
You can delete an existing index in a table with the DROP statement.
What if we only want to get rid of the data inside a table, and not the
table itself?
Use the TRUNCATE TABLE command (deletes only the data inside
the table):
Note:
Some database systems don't allow the dropping of a column in a
database table (DROP COLUMN column_name).
HAVING... was added to SQL because the WHERE keyword could not be used against
aggregate functions (like SUM), and without HAVING... it would be impossible to test for
result conditions.
The syntax for the HAVING function is:
This SQL:
SELECT Company,SUM(Amount) FROM SalesGROUP BY CompanyHAVING
SUM(Amount)>10000Returns this result:
A view contains rows and columns, just like a real table. The fields in a view are
fields from one or more real tables in the database. You can add SQL functions,
WHERE, and JOIN statements to a view and present the data as if the data were
coming from a single table.
Note: The database design and structure will NOT be affected by the functions,
where, or join statements in a view.
Syntax
CREATE VIEW view_name ASSELECT column_name(s)FROM
table_nameWHERE condition
Note: The database does not store the view data! The database engine recreates the
data, using the view's SELECT statement, every time a user queries a view.
A view could be used from inside a query, a stored procedure, or from inside
another view. By adding functions, joins, etc., to a view, it allows you to present
exactly the data you want to the user.
The sample database Northwind has some views installed by default. The view
"Current Product List" lists all active products (products that are not discontinued)
from the Products table. The view is created with the following SQL:
CREATE VIEW [Current Product List] ASSELECT
ProductID,ProductNameFROM ProductsWHERE Discontinued=No
We can query the view above as follows:
SELECT * FROM [Current Product