manual-testing-120-130
manual-testing-120-130
Example
Use OR to display each person with the first name equal to "Tove", or the last name equal to "Svendson":
Result:
SQL IN
IN
The IN operator may be used if you know the exact value you want to return for at least one of the columns.
SQL BETWEEN
BETWEEN ... AND
The BETWEEN ... AND operator selects a range of data between two values. These values can be numbers, text, or
dates.
Result:
LastName FirstName
Hansen Ola
Svendson Tove
Pettersen Kari
SQL Join
Joins and Keys
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:
Employee_ID Name
01 Hansen, Ola
02 Svendson, Tove
03 Svendson, Stephen
04 Pettersen, Kari
Result
Name Product
Hansen, Ola Printer
Svendson, Stephen Table
Svendson, Stephen Chair
Example
Who ordered a printer?
SELECT Employees.Name
FROM Employees, Orders
WHERE Employees.Employee_ID=Orders.Employee_ID
AND Orders.Product='Printer'
Result
Name
Hansen, Ola
Using Joins
OR we can select data from two tables with the JOIN keyword, like this:
Example INNER JOIN
Syntax
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
Name Product
Hansen, Ola Printer
Svendson, Stephen Table
Svendson, Stephen Chair
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.
Result
Name Product
Hansen, Ola Printer
Svendson, Tove
Svendson, Stephen Table
Svendson, Stephen Chair
Pettersen, Kari
Example RIGHT JOIN
Syntax
The RIGHT JOIN returns all the rows from the second table (Orders), even if there are no matches in the first table
(Employees). If there had been any rows in Orders that did not have matches in Employees, those rows also would have
been listed.
Result
Name Product
Hansen, Ola Printer
Svendson, Stephen Table
Svendson, Stephen Chair
SELECT Employees.Name
FROM Employees
INNER JOIN Orders
ON Employees.Employee_ID=Orders.Employee_ID
WHERE Orders.Product = 'Printer'
Result
Name
Hansen, Ola
SQL Statement 1
UNION
SQL Statement 2
Employees_Norway:
Employee_ID E_Name
01 Hansen, Ola
02 Svendson, Tove
03 Svendson, Stephen
04 Pettersen, Kari
Employes_USA:
Employee_ID E_Name
01 Turner, Sally
02 Kent, Clark
03 Svendson, Stephen
04 Scott, Stephen
Result
Name
Hansen, Ola
Svendson, Tove
Svendson, Stephen
Pettersen, Kari
Turner, Sally
Kent, Clark
Scott, Stephen
Note: This command cannot be used to list all employees in Norway and USA. In the example above we have two employees
with equal names, and only one of them is listed. The UNION command only selects distinct values.
UNION ALL
The UNION ALL command is equal to the UNION command, except that UNION ALL selects all values.
SQL Statement 1
UNION ALL
SQL Statement 2
Using the UNION ALL Command
Example
List all employees in Norway and USA:
Example
This example demonstrates how you can create a table named "Person", with four columns. The column names will be
"LastName", "FirstName", "Address", and "Age":
This example demonstrates how you can specify a maximum length for some columns:
The data type specifies what type of data the column can hold. The table below contains the most common data types in SQL:
If you want to index the values in a column in descending order, you can add the reserved word DESC after the column
name:
If you want to index more than one column you can list the column names within the parentheses, separated by
commas:
Note: Some database systems don't allow the dropping of a column in a database table (DROP COLUMN column_name).
Person:
LastName FirstName Address
Pettersen Kari Storgt 20
Example
To drop the "Address" column in the "Person" table:
ALTER TABLE Person DROP COLUMN Address
Result:
LastName FirstName City
Pettersen Kari
SQL Functions
SQL has a lot of built-in functions for counting and calculations.
Function Syntax
The syntax for built-in SQL functions is:
SELECT function(column) FROM table
Types of Functions
There are several basic types and categories of functions in SQL. The basic types of functions are:
· Aggregate Functions
· Scalar functions
Aggregate functions
Aggregate functions operate against a collection of values, but return a single value.
Note: If used among many other expressions in the item list of a SELECT statement, the SELECT must have a GROUP BY
clause!!
"Persons" table (used in most examples)
Name Age
Hansen, Ola 34
Svendson, Tove 45
Pettersen, Kari 19
GROUP BY Example
This "Sales" Table:
Company Amount
W3Schools 5500
IBM 4500
W3Schools 7100
Company SUM(Amount)
W3Schools 17100
IBM 17100
W3Schools 17100
Company SUM(Amount)
W3Schools 12600
IBM 4500
HAVING...
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.
This SQL: