Uploaded by Nikko Bowie Pascual

SQL

advertisement
Introduction To SQL
Web Application Architecture
Front End
amazon.com
Typed ‘Java book’ and
Hit search
Back End
Items.search(“java Book”);
Database
Runs SQL code
Result25,393
Web Application Architecture
Front End
HTML, CSS, JavaScript,
JQuery, LESS, SASS,
Bootstrap, CoffeeScript
And more. Angular.js
Front End Developer
UI UX Designer…
Back End
Database
PHP, Ruby, Python,
C#, Java, Node.Js,
Swift and more
BackEnd Developers
Server side developers
SQL
Database admins
Database developers
Web Application Architecture
Front End
Manuel Tester
Black box test
Selenium + Java + Cucumber
Back End
API Testing
API Testing Automation
ResAssured Library
Database
SQL
Database Tester
Automate Database
Test Using Java JDBC
What is Data ?
Piece of information
For example Bank account
Account Number ->123
Account Type -> Checking
User First name -> John
Last name -> Smith
Balance ->100,000
What is Data ?
All above data needs to be stored somewhere, where it is secure,
easy to ready, fast to read, easy and fast to update.
In databases we store data in an organized manner.
What is Database ?
Database is a systematic collection of data.
Databases support storage and manipulation of data.
Databases make data management easy.
Relational Database
Organize the data in a series of tables
Relational Database
Tables are related to each other using Primary and Foreign keys
Relational Database
Non Relational Database
All Data are in Key & Value format
Database Management System
Creates, maintains and organizes the database
Easier to manage the database
Improves the availability of the information
Does have backup system
RDBMS
RDBMS --> Relational Database Management System.
All RDBMS using SQL language
Relational Database --> tables are related to each other using
Primary and Foreign keys
WHAT IS SQL ?
What Is SQL ?
SQL - > STRUCTURED QUERY LANGUAGE
SQL is a language that is used to work with Databases and
manipulate data.
SQL
SQL is combined with four languages:
Data Query Language(DQL)
Data Definition Language (DDL)
Data Control Language (DCL)
Data Manipulation Language (DML):
What is Query in SQL ?
A set of instructions
Telling Database Management System that what
we would like to do.
SQL
Data Types in Query
SQL STATEMENTS
SELECT STATEMENT
First, we specify a list of columns in the table from which we want
to query data in the SELECT statement.
We use a comma between each column in case we want to
query data from multiple columns.
If we want to query data from all column, we can use an asterisk
( * ) as the shorthand for all columns.
Second, we indicate the table name after the FROM keyword SQL
language is case INSENSITIVE
SELECT STATEMENT
The following illustrates the syntax of the SELECT statement:
SELECT column1, column2… FROM table name ;
keyword
keyword
SELECT STATEMENT SYNTAX
SELECT * FROM TableName;
SELECT ColumnName
FROM TableName;
SELECT ColumnName1, ColumnName2 ... FROM TableName;
SELECT Column(s) FROM TableName1, TableName2 ;
SELECT DISTINCT STATEMENT
The DISTINCT keyword can be used to return only distinct (different) values.
SELECT DISTINCT column1, column2… FROM table name ;
Remove duplicate values
WHERE STATEMENT
The WHERE clause appears right after the FROM clause of the
SELECT statement.
The conditions are used to filter the rows returned from the SELECT
statement.
SQL provides us with various standard operators to construct the
conditions.
WHERE CLAUSE SYNTAX
SELECT column_1, column_2.. column_n
FROM table_name
WHERE conditions;
Applies filter to result
WHERE STATEMENT
SELECT WHERE Statement
WHERE
STATEMENT
OPERATORS
BETWEEN STATEMENT
We use the BETWEEN operator to match a value against a range of
values.
For example;
.
Value BETWEEN low AND high
BETWEEN STATEMENT
If the value is greater than or equal to the low value and less than or
equal to the high value, the expression returns true, or vice versa.
We can rewrite the BETWEEN operator by using the greater than or
equal ( >=) or less than or equal ( <=) operators as the following
statement:
.
value >= low AND value <= high
value BETWEEN low AND high
Same statement
IN STATMENT
We use the IN operator with the WHERE clause to check if a value matches
any value in a list of values.
The syntax of the IN operator is as follows:
value IN (value l, value2,...)
IN STATMENT
The list of values is not limited to a list of numbers or strings but also
a result set of a SELECT statement as shown in the following query:
Value IN (SELECT value FROM tbl_name)
Just like with BETWEEN, you can use NOT to adjust an IN statement (NOT IN)
ORDER BY STATEMENT
The ORDER BY clause allows you to sort the rows returned from the SELECT
statement in ascending or descending order based on criteria specified.
The following illustrates the syntax of the SELECT statement:
SELECT column 1, column 2
FROM table name
ORDER BY column_1 ASC / DESC;
The column name
we want order by
Which order we want
ASC or DESC
LIKE STATEMENT
Suppose the store manager asks you find an employee that he does
not remember the name exactly.
He just remembers that employee's first name begins with something
like Jen.
How do you find the exact employee that the store manager is asking?
LIKE STATEMENT
You may find the employee in the employee table by looking at the
first name column to see if there is any value that begins with Jen.
It is kind of tedious because there are many rows in the customer table.
LIKE STATEMENT
Fortunately, we can use the LIKE operator to as the following query:
SELECT first name, last name
FROM employee
Single Quotes
WHERE first name LIKE 'Jen%';
% = pattern
matching(take
whatever after
Jen)
LIKE STATEMENT
The query returns rows whose values in the first name column begin
with Jen and may be followed by any sequence of characters.
This technique is called pattern matching.
LIKE STATEMENT
You construct a pattern by combining a string with wildcard characters
and use the LIKE or NOT LIKE operator to find the matches.
Percent ( %) for matching any sequence of characters.
Underscore ( _ ) for matching any single character.
NOTE : PostgreSQL provides the ILIKE operator, that acts exactly
like the LIKE operator, except it values matches without case-sensitivity.
COUNT STATEMENT
The COUNT function returns the number of input rows that match
a specific condition of a query.
How many departments do we have ?
COUNT STATEMENT
The COUNT function returns the number of input rows that match
a specific condition of a query.
SELECT COUNT(*) FROM table name;
COUNT STATEMENT
Similar to the COUNT(*) function, the COUNT(column) function returns
the number of rows returned by a SELECT clause.
However, it does not consider NULL values in the column.
COUNT STATEMENT
We can also use COUNT with DISTINCT, for example;
How many unique name we have ?
AGGREGATE FUNCTIONS
Performs the action for multiple rows at once and returns single result
1. MIN
2. MAX
3. AVG
4. SUM
MIN
Checks all the rows and shows minimum one.
SELECT MIN (salary)
FROM employees;
Column Name
MAX
Checks all the rows and shows maximum one.
SELECT MAX (salary)
FROM employees;
Column Name
AVG
Add all rows and get average.
SELECT AVG (salary)
FROM employees;
Column Name
ROUND
ROUND the result with given decimal.
SELECT ROUND (AVG (salary),2)
FROM employees;
Column Name
How many decimals we want to see
SUM
Add all rows and shows SUM
SELECT SUM (salary)
FROM employees;
Column Name
GROUP BY
The GROUP BY clause divides the rows returned from the SELECT
statement into groups.
For each group, you can apply an aggregate function, for example:
calculating the sum of items
count the number of items in the groups.
GROUP BY
SELECT column_1, aggregate_function(column_2)
FROM table_name
GROUP BY column_1;
HAVING
We often use the HAVING clause in conjunction with the GROUP BY
clause to filter group rows that do not satisfy a specified condition.
SELECT column_1, aggregate_function(column_2)
FROM table_name
GROUP BY column_1;
HAVING condition(aggregate)
HAVING
The HAVING statement sets the condition for group rows created by
the GROUP BY clause after the GROUP BY clause applies while the WHERE
clause sets the condition for individual rows before GROUP BY clause
applies.
This is the main difference between the HAVING and WHERE clauses.
SUBQUERY
A subquery allows us to use multiple SELECT statements, where
we basically have a query within a query.
SUBQUERY
Suppose we want to find who is making highest salary. We can
do this in two steps:
Find highest salary with SELECT statement and MAX function
Use the result of first query as an input to second query where
the employee details are equal to result.
SUBQUERY
Instead we can combine this two query, basically we put second
query in brackets and use it in the WHERE clause as an input.
SELECT *
FROM employees
WHERE salary = (SELECT MAX(salary) FROM employees);
SUBQUERY
So inner query will be executed first, and the result will be used
as an input to outer query.
SELECT *
Outer Query
FROM employees
WHERE salary = (SELECT MAX(salary) FROM employees);
Inner Query
ROWNUM
Limits the number of result displayed in the query.
Only works with less than (<) and less then and equals (<=)
NOTE : Other databases like postgresql, mysql the keyword is LIMIT
and it comes after all clauses.
STRING FUNCTIONS
String || String -> String concatenation
CONCAT -> String concatenation
LOWER -> Convert string to lower case
UPPER-> Convert string to upper case
LENGTH -> number of character in string
SUBSTRING -> extract substring
INITCAP -> Makes first letters upper case
VIEWS
A VIEW in SQL Server is like a virtual table that contains data
from one or multiple tables.
It does not hold any data and does not exist physically in the
database.
CREATE VIEW tableName AS
CREATE VIEW personal_info AS
Type Query Here
SELECT first_name || ' ' || last_name
FROM employees;
DDL & DML
DDL&DML
DDL: Data Definition Language
DML: Data Manipulation Language
It is used to define data structures
It is used to define data itself
CREATE
SELECT
DROP
INSERT
TRUNCATE
UPDATE
ALTER
DELETE
CREATE TABLE
To create a new SQL table, you use the CREATE TABLE statement.
First, you specify the name of the new table after the CREATE TABLE
clause
CREATE TABLE table_name
(column name TYPE constraint);
CONSTRAINT is NOT mandatory to give
CREATE TABLE
Next, you list the column name, its data type, and column constraint.
You can have multiple columns in a table, each column is separated
by a comma (,). The column constraint defines the rules for the
column e.g., NOT NULL.
CREATE TABLE table_name
(column name TYPE constraint);
COLUMN CONSTRAINTS
NOT NULL — the value of the column cannot be NULL.
UNIQUE —the value of the column must be unique across the whole table.
PRIMARY KEY — Combination of both NOT NULL and UNIQUE constraints.
REFERENCES — OtherTable (PKColumn) — used to give foreign key to the
column. We give the foreign key to the column by making Other table’s
primary key column as the reference column
IN ORDER TO HAVE FORIGN KEY, WE MUST HAVE THE PRIMARY KEY IN OTHER TABLE
INSERT
When you create a new table, it does not have any data.
The first thing you often do is to insert new rows into the table.
SQL provides the INSERT statement that allows you to insert one
or more rows into a table at a time.
INSERT
To add multiple rows into a table at a time, you use the
following syntax:
INSERT INTO tableName (column1, column2,…)
VALUES (value1, value2 … );
The value list must be in the same order as the columns list
INSERT
After all the insertion is done, you have to commit them. If you
don’t commit, it will not be entered in the database
Syntax:
commit; or commit work;
We just need to do it one time at the end
UPDATE
To change the values of the columns in a table, you use the UPDATE
statement.
UPDATE table_name
SET column1 = value1,
column2 = value2 , …
WHERE condition;
UPDATE
First, specify the table name where you want to update the data
after UPDATE clause.
Second, list the columns whose values you want to change in the
SET clause.
Third, determine which rows you want to update in the condition
of the WHERE clause.
DELETE
To delete rows in a table, you use DELETE statement as follows:
Second, specify which row to delete by using the condition in
the WHERE clause.
If you omit the WHERE clause, all rows in the table are deleted.
DELETE FROM table_name
WHERE condition;
DELETE
The DELETE statement returns the number of rows deleted.
If no rows are deleted, the DELETE statement returns zero.
ALTER
To change existing table structure, you use ALTER TABLE
statement.
The syntax of the ALTER TABLE is as follows:
ALTER TABLE table_name action;
ALTER Actions
The popular actions that we can do with alter keyword:
ADD COLUMN : adds column to the table
DROP COLUMN : drops the column from the table
RENAME COLUMN : renames the column name
RENAME TO : renames the table name
TRUNCATE
Truncating will remove all data from the table but not
delete the table
TRUNCATE table_name;
DROP TABLE
To remove existing table from the database, you use the
DROP TABLE statement as shown following:
DROP TABLE table_name;
JOINS
JOINS
There are several kinds of joins including INNER JOIN, OUTER
JOIN and SELF-JOIN.
Suppose we want to get data from
Address and Customer tables. The
Customer table has the address_id
field that relates to the primary key of
the Address table.
INNER JOIN
To get data from both tables, we use the INNER JOIN clause
in the SELECT statement as follows:
Table Name . Column_name
SELECT customer.customer_id
FROM address INNER JOIN customer
ON address.address_id= customer.address_id
Primary Key
Foreign Key
INNER JOIN
Inner Join produces only the set of records that match in both
Table Customer and Table Address.
INNER JOIN
INNER JOIN
INNER JOIN
SELECT customer_id, first_name, last_name, address, phone
FROM customer INNER JOIN address
ON customer.address_id = address.address_id;
LEFT OUTER JOIN
Left outer join produces a complete set of records from Table
customer, with the matching records (where available) in Table
Address. If there is no match, the right side will contain null.
LEFT OUTER JOIN
LEFT OUTER JOIN
LEFT OUTER JOIN
SELECT * FROM customer LEFT OUTER JOIN address
ON customer.address_id = address.address_id;
C
A
LEFT OUTER JOIN
SELECT customer_id, first_name, last_name, phone
FROM customer LEFT OUTER JOIN address
ON customer.address_id = address.address_id;
C
A
LEFT OUTER JOIN WITH WHERE
To produces the set of records only in Customer table, but not in
Address table, we perform the same left outer join, then exclude the
records we don’t want from the right side via a where clause.
C
A
LEFT OUTER JOIN WITH WHERE
C
A
LEFT OUTER JOIN WITH WHERE
C
A
LEFT OUTER JOIN WITH WHERE
SELECT customer_id, first_name, last_name, phone
FROM customer LEFT OUTER JOIN address
ON customer.address_id = address.address_id
WHERE address.address_id IS NULL
C
A
RIGHT OUTER JOIN
Right outer join produces a complete set of records from Table Address,
with the matching records (where available) in Table Customer. If there is
no match, the left side will contain null.
C
A
RIGHT OUTER JOIN
C
A
RIGHT OUTER JOIN
C
A
RIGHT OUTER JOIN
C
A
RIGHT OUTER JOIN
SELECT customer_id, first_name, last_name, phone
FROM customer RIGHT OUTER JOIN address
ON customer.address_id = address.address_id;
C
A
FULL OUTER JOIN
Full outer join produces the set of all records in Table Customer and Table
Address with matching records from both sides where available. If there is no
match, the missing side will contain null.
C
A
FULL OUTER JOIN
C
A
FULL OUTER JOIN
C
A
FULL OUTER JOIN
SELECT * FROM customer FULL OUTER JOIN address
ON customer.address_id = address.address_id;
C
A
FULL OUTER JOIN
SELECT customer_id, first_name, last_name, address, phone
FROM customer FULL OUTER JOIN address
ON customer.address_id = address.address_id;
C
A
FULL OUTER JOIN WITH WHERE
To produces the set of records unique to Customer Table and Address
Table, we perform the same full outer join, then exclude the records we don't
want from both sides via a where clause.
C
A
FULL OUTER JOIN WITH WHERE
C
A
FULL OUTER JOIN WITH WHERE
SELECT * FROM customer LEFT OUTER JOIN address
ON customer.address_id = address.address_id
WHERE customer.address_id IS NULL OR
address.address_id IS NULL
C
A
FULL OUTER JOIN WITH WHERE
SELECT customer_id, first_name, last_name, address,
phone
FROM customer LEFT OUTER JOIN address
ON customer.address_id = address.address_id
WHERE customer.address_id IS NULL OR
address.address_id IS NULL
C
A
SELF JOIN
Join the table to itself.
We use self join when we want to combine rows with other
rows in the same table.
To perform the self join operation, we must use a table alias
to help SQL distinguish the left table from the right table of the
same table.
SELF JOIN
Let’s say we want to print out employee full name with their
manager’s name together.
SELECT e1.first_name, e1.last_name, e1.manager_id, e2.last_name
FROM employees e1 JOIN employees e2
ON e1.manager_id = e2. employee_id;
SET OPERATORS
SET OPERATORS
UNION, UNION ALL, INTERSECT, MINUS
For SET operators to work:
You need 2 independent queries
Same number of columns in Select statement
Same data type in same order
UNION
The UNION operator combines result sets of two or more SELECT
statements into a single result set.
SELECT column 1, column 2 … FROM Table1
UNION
SELECT column1, column 2 … FROM Table 2
Q1
Q2
UNION
The following are rules applied to the queries:
Both queries must return the same number of columns.
The corresponding columns in the queries must have
compatible data types.
Q1
Q2
UNION
The UNION operator removes all duplicate rows unless the UNION
ALL is used.
We often use the UNION operator to combine data from similar
tables that are not perfectly normalized.
Q1
Q2
UNION vs UNION ALL
UNION &
UNION ALL
=
MINUS
MINUS set operator returns records from first query that is
not present in second query.
It will only return values (from 1st query) that are not
common in 2 queries
Q1
Q2
MINUS
Let’s say we have two query result
D
T
MINUS
Let’s say we have two query result
MINUS
=
SELECT Name FROM Developers
MINUS
SELECT Name FROM Testers
D
T
INTERSECT
INTERSECT set operators returns records that are present/common/
appear in both query results .
It will sort and remove duplicates.
Q1
Q2
INTERSECT
Let’s say we have two query result
D
T
INTERSECT
Let’s say we have two query result
INTERSECT
SELECT Name FROM Developers
INTERSECT
SELECT Name FROM Testers
=
D
T
SUMMARY OF SET OPERATORS
UNION -> combines, removes duplicates, sorts
UNION ALL-> combines, does not remove duplicates, does not sort
MINUS -> show records from query1 that are not present in query2
INTERSECT -> show common records from 2 queries
Download