Insert Multiple. (Here variation 2 was used)
INSERT INTO
[(, , ..., )]
VALUES
(t1[], t1[], ..., t1[]),
(t2[], t2[], ..., t2[]),
...
(tm[], tm[], ..., tm[])
Basic statement for retrieving information from a database but they can get complex
Basic form of a Select Statement
select-from-where block
SELECT
FROM
WHERE ;
In the Query:
SELECT Fname, Lname, Salary
FROM EMPLOYEE, DEPARTMENT
WHERE EMPLOYEE.Dnumber = DEPARTMENT.Dnumber
AND Dname = 'Engineering';
The statement "EMPLOYEE.Dnumber = DEPARTMENT.Dnumber" is an example of a ?
In the Query:
SELECT Fname, Lname, Salary
FROM EMPLOYEE, DEPARTMENT
WHERE EMPLOYEE.Dnumber = DEPARTMENT.Dnumber
AND Dname = 'Engineering';
The statement Dname = 'Engineering' is an example of a?
Is used to remove ambiguity of queries
Best practice is to prefix
with aliases whether there's
ambiguity or none for better
query comprehension
are also useful to remove ambiguity in
relation names
WHERE clause is optional and without one, all tuples of the relation specified in the FROM clause are ___________ for the query
result
If there are more than one relation in the FROM clause, the ______________ (all possible tuple combinations) of these relations is
selected
Specify every selection and join condition needed because not doing so may result in incorrect and/or very large output
Used to retrieve all the attribute values of the selected tuples
Allows the use of a temporary, more readable name to an attribute / column within a query; It only exists for the duration of the query
TRUE or FALSE:
An Attribute Alias becomes permanent once assigned
FALSE. It only exists for the duration of
the query
TRUE or FALSE:
Tables in SQL may allow two or more tuples that are Identical
TRUE. tables in SQL may allow two or more
tuples that are identical, i.e., it may not be a set of tuples but rather a multiset
SQL does not automatically remove duplicate tuples in the results
of queries due to:
1. Duplicate elimination is an expensive operation
2. User may want to see duplicate tuples in the result of query
3. Usually, when using aggregate function/s against tuples, it is
undesirable to remove duplicates
_____________ keyword is used to explicitly remove duplicate tuples from result
__________, _____________, and ____________ operators perform a set union, set intersection, and set difference operation, respectively. That is, duplicates are eliminated
Adding ___________ keyword to the set operators will perform their corresponding multiset operation (i.e., duplicates are NOT eliminated)
Given the Tables:
test1 test 2
1 1
2 2
2 4
3 5
What is the output of this query?
SELECT id FROM test1 UNION SELECT id from test2;
Given the Tables:
test1 test 2
1 1
2 2
2 4
3 5
What is the output of this query?
SELECT id FROM test1 UNION ALL SELECT id from test2;
Given the Tables:
test1 test 2
1 2
2 2
2 3
3 4
What is the output of this query?
SELECT id FROM test1 INTERSECT SELECT id from test2;
Given the Tables:
test1 test 2
1 2
2 2
2 3
3 4
What is the output of this query?
SELECT id FROM test1 INTERSECT ALL SELECT id from test2;
The _________ comparison operator can be used for string pattern matching
______________ (matches zero or more characters) and (blank) (matches
exactly one character) may be used in the pattern string
Wildcards %, _ (underscore)
In case there is a need to match the % and/or _ characters, the ___________ clause is used. Default escape character is ___________.
Substring Pattern Matching where we retrieve the department names ending in "ing" from a table named DEPARTMENT
SELECT Dname from DEPARTMENT WHERE Dname LIKE '%ing';
Substring Pattern Matching where we use the ESCAPE clause
SELECT val from demo1 WHERE val LIKE 'UNDER$_%' ESCAPE '$'
here the dollar sign is assigned as an escape character for the _
Default Version would be:
SELECT val from demo1 WHERE val LIKE 'UNDER\_%';
Comparison operator that returns TRUE if value being compared is one of the elements in the given set of values
IN Operator
ex. SELECT Dnumber, Dname FROM DEPARTMENT WHERE Dnumber IN (1,3);
Comparison operator that returns TRUE if compared value is between two values (inclusive)
BETWEEN ... AND Operator
ex. SELECT Dnumber, Dname FROM DEPARTMENT WHERE Dnumber BETWEEN 2 AND 4;
The _____________ clause allows the user to order the tuples in the query result by the values of one or more attributes
The ordering can be specified as ascending (_____) or descending (______). If not specified, the default is ________.
ASC, DESC (default is ASC)
Adds zero or more tuples (rows) depending on the result of the query
INSERT Statement (when used for Copying Tables/Rows)
Sample INSERT Statement for Copying
INSERT INTO .... SELECT
INSERT INTO employee1 SELECT Ssn, Salary FROM Employee;
Creates a new table then adds zero or more tuples (rows) depending on the result of the query. Not all table components (e.g. some constraints) are copied in the new table and will have to be added manually after
CREATE TABLE .. SELECT
ex. CREATE TABLE employee2 SELECT Ssn, Salary FROM EMPLOYEE;
Removes zero or more rows from relation depending on the number of tuples selected
DELETE FROM
WHERE ;
ex. DELETE FROM EMPLOYEE WHERE Dnumber = 1 AND Super_ssn IS NOT NULL;
Modifies attribute values of zero or more tuples in relation
UPDATE
SET = , = , ..., =
WHERE ;
ex. UPDATE EMPLOYEE
SET Minit = NULL, Salaray = Salary * 1.1
WHERE Dnumber = 1;
When attribute(s) with NULL values are involved in comparisons, result is ____________
unknown (maybe true or false)
TRUE or FALSE:
SQL uses a binary-valued logic with values of TRUE and FALSE
FALSE. SQL uses a three-valued logic with values of TRUE, FALSE, and UNKNOWN
What will the boolean expression evaluate into?
TRUE and UNKNOWN
What will the boolean expression evaluate into?
FALSE and UNKNOWN
What will the boolean expression evaluate into?
TRUE or UNKNOWN
What will the boolean expression evaluate into?
FALSE or UNKNOWN
What will the boolean expression evaluate into?
NOT UNKNOWN
Referred to as subqueries in our Lab handout(s). Query contained inside another SQL query
Nested Queries
ex. SELECT * FROM table_Name WHERE
attribute IN (another Select Query)
Can be used in WHERE, FROM, SELECT clauses. Allows use of tuples of values in comparisons by placing them in
parentheses
__________ function is typically used with correlated nested queries; Returns TRUE if query returns at least one row
Correlated Nested Query for select all the department without employee
SELECT d.Dnumber, d.Dname FROM DEPARTMENT d WHERE NOT EXISTS (
SELECT Fname FROM EMPLOYEE WHERE Dnumber = d.Dnumber);
INNER JOIN, LEFT JOIN, AND NATURAL JOIN are examples of ?
If we intend to see the non-matching ones as well, we can use an _________
Every tuple in the left table must be in the result; Non-matching tuples will have NULL values for attributes of the right table.
Left Outer Join
ex. SELECT l.descr "LEFT", rdescr "RIGHT" FROM demo_left l LEFT OUTER JOIN demo_right r ON (l.id = r.id);
TRUE or FALSE:
RIGHT JOIN and RIGHT OUTER JOIN are synonymous
TRUE. The outer keyword is optional. Both clauses perform the same operation, that is returning all rows from the right table and matching rows from the left table, with NULLs for unmatched columns from the left table
Every tuple in the right table must be in the result; Non-matching
tuples will have NULL values for attributes of the left table.
Every tuple in the left and right tables must be in the result; Non- matching tuples will have NULL values for attributes of the non-
matching side.
TRUE or FALSE:
MariaDB does not have a full outer join implementation but can be done with doing a UNION ALL of the left and right outer joins
TRUE. Full Outer Join can be achieved via the UNION ALL of the left and right outer join
ex. select * from emp right join dept on emp.deptno = dept.deptno UNION select * from emp left join dept on emp.deptno = dept.deptno;
Same as joining tables without the join condition/s; Results in a cross product of the tuples.
Can be used when a value can be different based on certain conditions
Used to summarize information from multiple tuples into a single-tuple summary
Built-in aggregate functions:
1. COUNT
2. SUM
3. MAX
4. MIN
5. AVG
TRUE or FALSE:
Aggregate Functions eliminate duplicate tuples
FALSE. Aggregate Functions DOES NOT eliminate duplicate tuples considered for aggregation
TRUE or FALSE:
Aggregate Functions DOES eliminate NULLs before aggregating
TRUE. Except for COUNT(*)
Allows subgroups of tuples to be formed in a relation; The subgroups are based on some attribute values
TRUE or FALSE:
Aggregate function(s) is (are) applied to the subgroup/s
SELECT Dno,
AVG(Salary) AvgSal
FROM EMPLOYEE
GROUP BY Dno;
Will return the average salary of all employees grouped by Dno
Can appear in conjunction with GROUP BY to provide conditions on which subgroups' summary information will be included in the
result
HAVING Clause
ex. SELECT Dno,
AVG(Salary) AvgSal
FROM EMPLOYEE
GROUP BY Dno
HAVING `AvgSal` > 1000;
Allows a user to define a table that will only be used in a particular query;
WITH Construct A.k.a. Common Table Expression (CTE)
SQL Queries Order of Precedence
[ WITH
]
SELECT
FROM
[ WHERE ]
[ GROUP BY ]
[ HAVING ]
[ ORDER BY ];
WITH RECURSIVE nextweek (dt) AS (SELECT CURDATE () + INTERVAL 1 DAY AS dt UNIION
SELECT dt + INTERVAL 1 DAY FROM nextweek
WHERE dt + INTERVAL 1 DAY < CURDATE() + INTERVAL 8 DAY)
Generate all dates in the next
7 days (not including today)
Can be used to specify action(s) to perform when certain events occur and when certain conditions are satisfied
Used to enforce application-based constraints (remember from Relational Model Constraints topic)
Triggers (CREATE TRIGGER statement)
Example use of CREATE Trigger
CREATE TRIGGER monite_salary
BEFORE INSERT ON EMPLOYEE
FOR EACH ROW
BEGIN
IF NEW.Salary > (SELECT Salary FROM EMPLOYEE
WHERE Ssn = NEW.Super_ssn)
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = "Salary Value Error";
END IF;
Single table derived from other tables
View
ex. CREATE VIEW EMP_PROJ AS
SELECT QUERY
View does not necessarily exist in physical form; Also referred to as ___________
Two implementation strategies for Views:
1. Query Modification
2. View Materialization
Involves transforming the view query into a query on the underlying base tables. No physical table. Inefficient for views defined via complex queries that are time-
consuming to execute
Involves physically creating a temporary or permanent view table (called a _________________)
View Materialization, materialized view table
Efficient strategy for automatically updating the view table when the base tables are updated must be developed
Views can be used to hide certain attributes or tuples from unauthorized users
Views as Authorization Mechanisms
Used to change the definition of base table or of other named schema elements
ALTER Statement
Examples:
ALTER TABLE .. ADD COLUMN .. ;
ALTER TABLE .. DROP COLUMN .. ;
ALTER TABLE .. ALTER .. ;
ALTER VIEW .. AS .. ;
ALTER SCHEMA .. ;
Used to remove base tables or other named schema elements
DROP Statement
ex:
DROP TABLE .. ;
DROP VIEW .. ;
DROP TRIGGER .. ;
DROP SCHEMA .. ;
Empties a table completely, often quicker than DELETE (all rows)
TRUNCATE TABLE Statement
Syntax: TRUNCATE TABLE