SQL Flashcards

(38 cards)

1
Q

Benefits of IN

A

IN executes faster than OR

Can contain another SELECT

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

NOT

A

WHERE NOT city = ‘London’ AND NOT city = ‘Dallas’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

OR

A

WHERE name = ‘ali’ OR ‘mamad’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

IN

A

WHERE id IN (9,10,11)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Wildcards

%

A

‘%Pizza’
‘Pizza%’
‘%Pizza%’
‘S%E’

WHERE size LIKE ‘%pizza’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

ORDER BY position

A

Must always be the last clause in a select statement

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Aggregate Functions

A
AVG()
COUNT()
MIN()
MAX()
SUM()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Count all the rows in a table containing values and NULL values

A
SELECT COUNT (*) AS total_cust
FROM customers;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Count all the rows in a specific column ignoring NULL vales

A
SELECT COUNT (customerID) AS total_cust
FROM customers;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

DISTINCT on aggregate functions

A
SELECT COUNT(DISTINCT customerID)
FROM customers
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Every column in your SELECT statement must be present in a GROUP BY clause, except for

A

aggregated calculations

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

WHERE filters on rows, instead use … to filter for groups

A

HAVING

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

WHERE vs HAVING

A

WHERE filters before data is grouped
HAVING filters after data is grouped
Rows eliminated by the WHERE clause will not be included in the group

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

HAVING

A

SELECT customerID, COUNT() AS orders
FROM orders
GROUP BY customerID
HAVING COUNT (
)>=2;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

WHERE clause operators

A
= 
<>   !=
>=   >
<=   <
BETWEEN AND
IS NULL
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Subquery selects can only retrieve a single column

17
Q

Cartesian (Cross) join

A

Each row from the first table joins with all the rows of another table.
Output will be the number of joins in the 1st table multiplied by the number of rows in the 2ed table

Ex.
SELECT *
FROM table1 CROSS JOIN table2

18
Q

inner joins

A

SELECT suppliers.CompanyName, ProductName, UnitPrice
FROM Suppliers INNER JOIN Products
ON Suppliers.supplierid = Products.supplierid

19
Q

Inner join in multiple tables?

A

Yes, no limit

20
Q

Inner join with multiple tables example

A

SELECT o.order_id, c.company_name ,e.last_name

FROM ((Orders o INNER JOIN Customers c ON o.customer_id = c.customer_id)
INNER JOIN Employees e ON o.employee_id = e.employee_id);

21
Q

Aliases

/ˈālēəs/

A

SELECT vendor_name ,product_name, product_price
FROM Vendors AS v, Products as p
WHERE v.vendor_id = p.vendor_id;

22
Q

Self Joins

A

SELECT
a.customer_name AS c_name1
,b.customer_name AS c_name2
a.city

FROM Customers AS a, Customers as b

WHERE a.customer_id = b.customer_id
AND a.city = b.city

ORDER BY a.city;

23
Q

FULL OUTER JOIN

A

SELECT C.customer_name , O.order_id

FROM Customers AS C

FULL OUTER JOIN Orders AS O
ON C.customer_id = O.customer_id

ORDER BY C.customer_name;

24
Q

Unions

A

A UNION is used to combine the result-set of multiple SELECT statements.

To create a union:

each SELECT statement must have the same number of columns

the columns in each SELECT statement must be in the same order

the matching columns must have similar data types

25
Union example
SELECT city, country FROM Customers WHERE country = 'Germany' UNION SELECT city, country FROM Suppliers WHERE country = 'Germany' ORDER BY city
26
Concatenation
SELECT company_name ,contact_name ,compaany_name || ' (' || contact_name || ')' FROM Customers
27
Trimming String
TRIM RTRIM LTRIM SELECT TRIM(" You the best ") AS trimmed_string;
28
Substring
-- SUBSTR([string name], [start pos], [num return chars]) SELECT first_name, SUBSTR(first_name, 2, 3) FROM Employees WHERE department_id = 69;
29
Upper and Lower
``` -- make string uppercase SELECT UPPER(first_name) FROM Customers -- or SELECT UCASE(first_name) FROM Customers ``` ``` -- make string lowercase SELECT LOWER(first_name) FROM Customers ```
30
STRFTIME
-- get the year, month, and day of each employee ``` SELECT birthdate , STRFTIME('%Y', birthdate) AS year , STRFTIME('%m', birthdate) AS month , STRFTIME('%d', birthdate) AS day FROM employees ```
31
Compute current date
SELECT DATE('now')
32
Compute dates and times for the current date
``` -- date SELECT STRFTIME('%Y %m %d', 'now') ``` ``` -- time SELECT STRFTIME('%H %M %S %s', 'now') ``` ``` -- compute employees' ages SELECT birthdate ,STRFTIME('%Y', birthdate) AS year ,STRFTIME('%m', birthdate) AS month ,STRFTIME('%d', birthdate) AS day ,DATE(('now') - birthdate) AS age FROM employees ```
33
Case Statements
Case statements in SQL mimic if-else statements in general programming. Case statements can be used in any clause that accepts a valid expression, e.g. WHERE ORDER BY HAVING and in statements: SELECT INSERT UPDATE DELETE
34
Case structure
``` CASE [input_expression] WHEN c1 THEN e1 WHEN c2 THEN e2 ELSE [e3] END [column_name] ```
35
Case example
SELECT employee_id ,firstname ,lastname ,CASE city WHEN 'Calgary' THEN 'Calgary' ELSE 'Other' END calgary FROM Employees ORDER BY lastname, firstname;
36
If ELSE is omitted and no WHEN statements evaluate to true,
NULL will be returned. As with programming languages, CASE in SQL will short-circuit on a true statement, and no further statements will be evaluated.
37
Search case statement
By omitting the optional input expression that directly follows the CASE keyword one can use the WHEN statements to evaluate and compare values: SELECT track_id ,name ,bytes ``` ,CASE WHEN bytes < 30000 THEN 'small' WHEN bytes >= 30001 AND bytes <= 50000 THEN 'medium' WHEN bytes > 50000 THEN 'large' ELSE 'Other' END bytes_category ``` FROM Tracks;
38
Views
With a view, one can add or remove columns without changing the schema. Views persist until the database connection is closed. --------------------------------------------------------------------------- -- create a view called my_view where... CREATE VIEW my_view AS ``` SELECT r.region_description ,t.territory_description ,e.lastname ,e.firstname ,e.hire_date ,e.reports_to ``` FROM Regions r INNER JOIN Territories t ON r.region_id = t.region_id INNER JOIN EmployeeTerritories et ON t.territory_id = et.territory_id INNER_JOIN Employees e on et.employee_id = e.employee_id -------------------------------------------------------------------------- To actually use the view, one treats it as if it were an existing table: ``` SELECT COUNT(territory_description) ,firstname ,lastname FROM my_view GROUP BY lastname, firstname; --------------------------------------------------------------------------- If a view is no longer required, one should clean up by DROPping the view. ``` DROP VIEW my_view; ----------------------------------------------------------------------- Views can be useful in environments where you're unable to write data to tables.