Operators and Statements Flashcards
(31 cards)
_
Underscore
Wildcard in LIKE statements, good for matching all characters but one.
SELECT * FROM FRIENDS WHERE last_name LIKE ‘_T’;
%
Modulo - In math calculations it gives remainder of a division problem.
Serves as wildcard in LIKE statements: SELECT * FROM —- WHERE —- LIKE ‘%_%’;
|| / CONCAT
Double Pipe: Concatenates two strings.
SELECT LASTNAME || ‘,’ || FIRSTNAME NAME FROM EMPLOYEES;
If employees accrue 12 days/yr, find all employees whose names start with B and who have more than 50 days of leave.
Lastname Years Leavetaken
SELECT LASTNAME, YEARS * 12 - LEAVETAKEN REMAINING
FROM VACATION
WHERE LASTNAME LIKE ‘B%’
AND YEARS * 12 - LEAVETAKEN > 50;
Logical Operators
AND, OR, NOT
UNION / UNION ALL
UNION: returns results of two queries and removes duplicate rows.UNION ALL: doesn’t remove duplicates.
SELECT * FROM –
UNION
SELECT * FROM –;
INTERSECT
Returns rows found in both queries.
SELECT * FROM –
INTERSECT
SELECT * FROM –;
MINUS
Returns rows from first query that were not present in second:
SELECT * FROM –
MINUS
SELECT * FROM –;
IN
Two ways:
SELECT * FROM – WHERE STATE = ‘TX’ OR STATE = ‘CA’;
SELECT * FROM – WHERE STATE IN (‘TX’, ‘CA’);
BETWEEN
Provides range. Two ways:
SELECT * FROM – WHERE PRICE > 10 AND PRICE < 20;
SELECT * FROM – WHERE PRICE BETWEEN 10 AND 20;
(Inclusive)
COUNT
Returns number of rows that satisfy condition in WHERE clause.
SELECT COUNT(*) alias FROM – WHERE –;
SUM
Returns sum of all values in a column. Only works on numbers.
SELECT SUM(COST) TOTAL FROM GROCERYLIST;
AVG
Average to take average of two columns:
SELECT AVG(HITS) / AVG(AB) TEAM_AVERAGE FROM TEAMSTATS;
MAX / MIN
MAX: Will return highest number in a column or word closest to Z.
MIN: opposite
ABS
Returns absolute value of a number.
CEIL / FLOOR
CEIL returns the smallest integer greater than or equal to its argument. 5.3 -> 6
FLOOR does opposite. 5.3 -> 5
INITCAP / LOWER / UPPER
INITCAP: Capitalizes first letter of a word, lowercases the rest.
LOWER/UPPER: Lowercases/uppercases all letters.
LPAD / RPAD
Left or right padding.
SELECT LASTNAME LPAD (LASTNAME, 1, ‘• ‘) FROM –;
Returns -> • Felts
LTRIM / RTRIM
Trims characters or blanks off a word.
SELECT LASTNAME, RTRIM (LASTNAME) FROM TABLE;
Then concatenate w ‘,’ Firstname
REPLACE
Arguments: (string to search, search key, replacement string)
If replacement argument is null, will remove each occurrence of the search string.
SELECT LASTNAME, REPLACE(LASTNAME, ‘F’, ‘E’) REPLACEMENT FROM TABLE;
SUBSTR
Allows you to take out a piece of the target string.
2-3 arguments: (target string, position of first character to be output, # of characters to show)
SELECT LASTNAME, SUBSTR (LASTNAME, 1, 3) FROM TABLE;
Shows first three letters of last name.
Negative second argument counts from right end of data.
LENGTH
Returns length of data input.
SELECT LASTNAME, LENGTH(RTRIM(LASTNAME)) FROM TABLE;
STARTING WITH
Clause just like: LIKE (‘A%’);
SELECT NAME FROM TABLE
WHERE NAME STARTING WITH (‘A’)
OR AGE LIKE(‘3_’);
ORDER BY
Sorts column in ascending order. Use DESC at end of statement to sort data in descending order.
SELECT NAME, AGE FROM TABLE
ORDER BY NAME ASC, AGE DESC;