SQL Commands Flashcards

This is a list of commands and Syntax (38 cards)

1
Q

SELECT

A

The SELECT statement is used to select data from a database.

SQL SELECT Syntax
SELECT column_name,column_name
FROM table_name;

and

SELECT * FROM table_name;

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

DISTINCT

A

In a table, a column may contain many duplicate values; and sometimes you only want to list the different (distinct) values.

SQL SELECT DISTINCT Syntax
SELECT DISTINCT column_name,column_name
FROM table_name;

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

WHERE

A

SQL WHERE Syntax
SELECT column_name,column_name
FROM table_name
WHERE column_name operator value;

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

AND, OR

A

The AND operator displays a record if both the first condition AND the second condition are true.

The OR operator displays a record if either the first condition OR the second condition is true.
Example

SELECT * FROM Customers
WHERE Country=’Germany’
AND City=’Berlin’;

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

ORDER BY

A

The ORDER BY keyword is used to sort the result-set by one or more columns.

SQL ORDER BY Syntax
SELECT column_name, column_name
FROM table_name
ORDER BY column_name ASC|DESC, column_name ASC|DESC;

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

Wild Card

A

In SQL, wildcard characters are used with the SQL LIKE operator.

SQL wildcards are used to search for data within a table.

With SQL, the wildcards are:

%
A substitute for zero or more characters
_
A substitute for a single character
[charlist]
Sets and ranges of characters to match
[^charlist]
or
[!charlist]
Matches only a character NOT specified within the brackets

Example
SELECT * FROM Customers
WHERE City LIKE ‘ber%’;
Example
SELECT * FROM Customers
WHERE City LIKE ‘%es%’;Example
SELECT * FROM Customers
WHERE City LIKE ‘_erlin’;

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

IN

A

The IN operator allows you to specify multiple values in a WHERE clause.

SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1,value2,…);

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

Alias

A

SQL aliases are used to give a database table, or a column in a table, a temporary name.

Basically aliases are created to make column names more readable.

SQL Alias Syntax for Columns
SELECT column_name AS alias_name
FROM table_name;
SQL Alias Syntax for Tables
SELECT column_name(s)
FROM table_name AS alias_name;

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

INNER JOIN

A

The INNER JOIN keyword selects all rows from both tables as long as there is a match between the columns in both tables.

SQL INNER JOIN Syntax
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;
or:

SELECT column_name(s)
FROM table1
JOIN table2
ON table1.column_name=table2.column_name;

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

LEFT JOIN

A

The LEFT JOIN keyword returns all rows from the left table (table1), with the matching rows in the right table (table2). The result is NULL in the right side when there is no match.

SQL LEFT JOIN Syntax
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name=table2.column_name;
or:

SELECT column_name(s)
FROM table1
LEFT OUTER JOIN table2
ON table1.column_name=table2.column_name;

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

RIGHT JOIN

A

The RIGHT JOIN keyword returns all rows from the right table (table2), with the matching rows in the left table (table1). The result is NULL in the left side when there is no match.

SQL RIGHT JOIN Syntax
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name=table2.column_name;
or:

SELECT column_name(s)
FROM table1
RIGHT OUTER JOIN table2
ON table1.column_name=table2.column_name;

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

full JOIN

A

The FULL OUTER JOIN keyword returns all rows from the left table (table1) and from the right table (table2).

The FULL OUTER JOIN keyword combines the result of both LEFT and RIGHT joins.

SQL FULL OUTER JOIN Syntax
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name=table2.column_name;

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

UNION

A

The UNION operator is used to combine the result-set of two or more SELECT statements.

Notice that each SELECT statement within the UNION must have the same number of columns. The columns must also have similar data types. Also, the columns in each SELECT statement must be in the same order.

SQL UNION Syntax
SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;
Note: The UNION operator selects only distinct values by default. To allow duplicate values, use the ALL keyword with UNION.

SQL UNION ALL Syntax
SELECT column_name(s) FROM table1
UNION ALL
SELECT column_name(s) FROM table2;

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

HAVING

A

The HAVING clause was added to SQL because the WHERE keyword cannot be used with aggregate functions.

HAVING Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);

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

LIMIT

A

The LIMIT clause is used to specify the number of records to return.

The LIMIT clause is useful on large tables with thousands of records. Returning a large number of records can impact performance.

LIMIT Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
LIMIT number;

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

CASE

A

The CASE statement goes through conditions and returns a value when the first condition is met (like an if-then-else statement). So, once a condition is true, it will stop reading and return the result. If no conditions are true, it returns the value in the ELSE clause.

If there is no ELSE part and no conditions are true, it returns NULL.

CASE Syntax
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
WHEN conditionN THEN resultN
ELSE result
END;

17
Q

LENGTH

A

The LENGTH() function returns the length of a string (in bytes).

Syntax
LENGTH(string)

18
Q

UPPER

A

The UPPER() function converts a string to upper-case.

Note: Also look at the LOWER() function.

Syntax
UPPER(text)

19
Q

LOWER

A

Convert the text to lower-case:

SELECT LOWER(‘SQL Tutorial is FUN!’);

20
Q

TRIM

A

Remove leading and trailing spaces from a string:

SELECT TRIM(‘ SQL Tutorial! ‘) AS TrimmedString;

SELECT LTRIM(‘ SQL Turorial! ‘) AS LeftTrimmedString;

SELECT RTRIM(‘ SQL Turorial! ‘) AS RightTrimmedString;

21
Q

RIGHT

A

Extract 3 characters from a string (starting from right):

SELECT RIGHT(‘SQL Tutorial’, 3) AS ExtractString;

22
Q

LEFT

A

Extract 3 characters from a string (starting from left):

SELECT LEFT(‘SQL Tutorial’, 3) AS ExtractString;

23
Q

SUBSTRING

A

Extract 3 characters from a string, starting in position 1:

SELECT SUBSTRING(‘SQL Tutorial’, 1, 3) AS ExtractString;

24
Q

REPLACE

A

Replace “T” with “M”:

SELECT REPLACE(‘SQL Tutorial’, ‘T’, ‘M’);

25
LOCATE
Search for "3" in string "W3Schools.com", and return position: SELECT LOCATE("3", "W3Schools.com") AS MatchPosition;
26
CONCAT
Add two strings together: SELECT CONCAT('W3Schools', '.com');
27
GROUP BY
The GROUP BY statement groups rows that have the same values into summary rows, like "find the number of customers in each country". The GROUP BY statement is often used with aggregate functions (COUNT(), MAX(), MIN(), SUM(), AVG()) to group the result-set by one or more columns. GROUP BY Syntax SELECT column_name(s) FROM table_name WHERE condition GROUP BY column_name(s) ORDER BY column_name(s);
28
EXTRACT
The EXTRACT() function extracts a part from a given date. Syntax EXTRACT(part FROM date)
29
Create a Temporary Table
With new_table_name AS ( SELECT Column FROM table_name_from WHERE filter_required )
30
What are the five SQL aggregate functions
SUM adds together all the values in a particular column. MIN returns the lowest value in a particular column MAX returns the highest value in a particular column AVG calculates the average of a group of selected values. COUNT counts how many rows are in a particular column.
31
ABS()
Calculating Absolute Differences Imagine you're tasked with preparing a dashboard showcasing the historical performance of Google's stock (ticker: 'GOOG') throughout 2023. The finance team specifically requests the absolute difference between the opening and closing prices for each trading day. We can utilize the ABS() function which calculates the absolute value of a number, irrespective of its sign. Syntax: SELECT date, ticker, (close-open) AS difference, ABS(close-open) AS abs_difference FROM stock_prices WHERE EXTRACT(YEAR FROM date) = 2023 AND ticker = 'GOOG';
32
ROUND()
Rounding Numbers Consider a scenario where you're compiling a report displaying the average closing prices of Apple's stock (ticker: 'AAPL') throughout 2022. To ensure the report's readability, you decide to round the average prices to 2 decimal places. The ROUND() function is handy here; it rounds a number to a specified number of decimal places. Syntax: SELECT ticker, AVG(close) AS avg_close, ROUND(AVG(close), 2) AS rounded_avg_close FROM stock_prices WHERE EXTRACT(YEAR FROM date) = 2022 GROUP BY ticker;
33
CEIL() and FLOOR()
Up and Down Now, imagine you're analyzing Facebook's stock performance (ticker: 'META') to identify potential trading ranges. We can utilize the CEIL() function to round up and the FLOOR() function to round down to the lowest and highest prices. Syntax: SELECT date, ticker, high, CEIL(high) AS resistance_level, low, FLOOR(low) AS support_level FROM stock_prices WHERE ticker = 'META' ORDER BY date;
34
CEIL() and FLOOR()
Up and Down Now, imagine you're analyzing Facebook's stock performance (ticker: 'META') to identify potential trading ranges. We can utilize the CEIL() function to round up and the FLOOR() function to round down to the lowest and highest prices. Syntax: SELECT date, ticker, high, CEIL(high) AS resistance_level, low, FLOOR(low) AS support_level FROM stock_prices WHERE ticker = 'META' ORDER BY date;
35
POWER()
Squared Values In this scenario, we'll explore the concept of squared values by focusing on the 3 popular tech stocks: Apple ('AAPL'), Google ('GOOG'), and Microsoft ('MSFT'). Although this scenario is simplified for illustrative purposes, it offers a straightforward way to introduce the usage of the POWER() function. The POWER() function raises a number to a specified power, allowing us to perform exponential calculations. Syntax: SELECT date, ticker, close, ROUND(POWER(close, 2),2) AS squared_close FROM stock_prices WHERE ticker IN ('AAPL', 'GOOG', 'MSFT') ORDER BY date;
36
MOD()
Imagine you're intrigued by stocks that exhibit a consistent price behavior divisible by 5. The MOD() function or % modulo operator calculates the remainder of division between two numbers. Syntax: SELECT ticker, close, MOD(close, 5) AS price_remainder_mod, close%5 AS price_remainder_modulo FROM stock_prices WHERE ticker = 'GOOG';
37
CREATE DATABASE
CREATE DATABASE database name; CREATE (object) ( name of object)
38