SQL Boot Camp
SQL Boot Camp
SQL Boot Camp
What is Databases?
A collection of data with accessible interface.
AS operator:
As operator helps in creating alias and it also works on functions.
The AS operator gets executed at the end of the query so we cannot
use AS operator inside WHERE and GROUP BY operator.
EX:
SELECT COUNT(*)
AS transaction
FROM payment
Where Clause:
- Appears right after from clause of the select statement
- A query with where clause evaluates where condition and only
returns those rows that satisfy the condition
- It is evaluated after from and before select and order by
- Can be used with UPDATE AND DELETE STATEMENTS
REPLACE:
The REPLACE() function, as well as the other string functions, only
change the query output, they don't affect the actual data in the
database.
REVERSE:
reverses a string in SQL
OR
DISTINCT:
1. SELECT DISTINCT CONCAT(author_fname,' ', author_lname) FROM books;
ORDER BY:
1. SELECT title FROM books ORDER BY title;
LIMIT:
SELECT
*
FROM
table
LIMIT n OFFSET m;
The statement first skips m rows before returning n rows generated by the query. If m is
zero, the statement will work like without the OFFSET clause.
EX: SELECT
first_name,
last_name
FROM
customer
WHERE
first_name LIKE '%er%'
EX: SELECT
first_name,
last_name
FROM
customer
WHERE
first_name NOT LIKE 'Jen%';
EX: SELECT
first_name,
last_name
FROM
customer
WHERE
first_name ILIKE 'BAR%';
BETWEEN:
Between operator matches value from a rang of values
Ex:
SELECT
customer_id,
payment_id,
amount
FROM
payment
WHERE
amount NOT BETWEEN 8 AND 9;
EX:
SELECT
customer_id,
payment_id,
amount,
payment_date
FROM
payment
WHERE
payment_date BETWEEN '2007-02-07' AND '2007-02-15';
IN:
In operator is used with Where clause to check if a value matches
any value in the list of values.
SELECT customer_id,
rental_id,
return_date
FROM
rental
WHERE
customer_id IN (1, 2)
NOT IN:
NOT combined with IN operator gives all the value which are not in
the list.
EX:
SELECT
customer_id,
rental_id,
return_date
FROM
rental
WHERE
customer_id NOT IN (1, 2);
Aggregate functions:
STRING_AGG():The PostgreSQL STRING_AGG() function is an
aggregate function that concatenates a list of strings and places a
separator between them. The function does not add the separator at
the end of the string.
STRING_AGG ( expression, separator [order_by_clause] ):
expression is any valid expression that can resolve to a character
string. If you use other types than character string type, you need
to explicitly cast these values of that type to the character string
type.
separator is the separator for concatenated strings.
The order_by_clause is an optional clause that specifies the order
of concatenated results
EX:
SELECT
country,
STRING_AGG (email, ';') email_list
FROM
customer
Ex: SELECT
ROUND(AVG(replacement_cost),2)
FROM
film;
Count: The COUNT(*) function returns the number of rows returned by a
SELECT statement, including NULL and duplicates.
EX: SELECT
COUNT(*)
FROM
table_name
WHERE
condition;
COUNT(DISTINCT column)
In this form, the COUNT(DISTINCT column) returns the number of unique non-null
values in the column.
GROUP BY:
It Is an aggregate function
The GROUP BY clause divides the rows returned from the SELECT
statement into groups. For each group, you can apply an aggregate
function e.g., SUM() and COUNT().
EX:
SELECT
column_1,
column_2,
aggregate_function(column_3)
FROM
table_name
GROUP BY
column_1,
column_2;
SELECT
customer_id,
SUM (amount)
FROM
payment
GROUP BY
customer_id;
Ex: SELECT
column_1,
aggregate_function (column_2)
FROM
tbl_name
GROUP BY
column_1
HAVING
condition;
SELECT
customer_id,
SUM (amount)
FROM
payment
GROUP BY
customer_id
HAVING
SUM (amount) > 200;
Cross joins:
A CROSS JOIN clause allows you to produce a Cartesian Product of
rows in two or more tables.
Select * from customers, orders is a typical example of cross join
where each row of a each table is multiplied to each row of other
table.
EX:
SELECT *
FROM T1
CROSS JOIN T2;
OR
SELECT *
FROM T1, T2;
Inner Joins:
SELECT
customer.customer_id,
first_name,
last_name,
email,
amount,
payment_date
FROM
customer
INNER JOIN payment ON payment.customer_id = customer.customer_id;
Ex: SELECT
customer.customer_id,
first_name,
last_name,
email,
amount,
payment_date
FROM
customer
INNER JOIN payment ON payment.customer_id = customer.customer_id
ORDER BY
customer.customer_id;
SELF JOINS:
A self-join is a query in which a table is joined to itself. Self-joins are useful for
comparing values in a column of rows within the same table.
SQL triggers:
SQL statements that are AUTOMATICALLY RUN when a
specific table is changed.
EX:
CREATE TRIGGER trigger_name
trigger_time trigger_event ON table_name FOR EACH
ROW
BEGIN
...
END;
Outer Joins:
EX:
LEFT OUTER JOIN:
Here the order of mentioning the table is necessary.
EX:
The left outer join with where clause can be used to get all the
rows that are unique to only Table A.
EX:
SELECT * FROM
Registrations LEFT OUTER JOIN Logins
WHERE
Logins.log_id is null
UNIONS:
It is used to combine the result of two or more SELCT statements.
We can add two columns as a primary key by passing the column names
to primary key constraints as shown below
PRIMARY KEY (column_1, Column_2)
We can also define primary key while altering the current table
structure.
EX:
CREATE TABLE products (
product_no INTEGER,
description TEXT,
product_cost NUMERIC
);
We can also drop the primary key constraint by using the command of
DROP CONSTRAINT.
ALTER TABLE products
DROP CONSTRAINT product_no;
Foreign Key:
Advanced SQLs:
Recursive Query:
With statement allows to construct auxiliary statements for the use
in a query.
These statements are often referred to as CTE (common table
expressions). A recursive CTE refers to itself, and it repeatedly
executes and returns subset of data, until it returns the complete
result set.
It is mostly used to query hierarchical data such as organization
charts and components where each one itself has further more
components.
WITH expression_name (column_list)
AS
(
-- Anchor member
initial_query
UNION ALL
-- Recursive member that references expression_name.
recursive_query
)
-- references expression name
SELECT *
FROM expression_name