SQL 3 Flashcards
Querying the EMP table, Use the IN clause to return all employees who started (STARTDATE) in 2017 and 2019.
Only want to see NAME and DEPT columns.
SELECT name, dept
FROM EMP
WHERE startdate IN (2017, 2019);
The IN Clause reduces the amount of text in a query by replacing what clause?
The OR Clause
Using the IN clause rewrite this query reducing the amount of SQL used
SELECT * FROM EMP WHERE firstname = ‘Peter’ OR Firstname = ‘Steven’ OR Firstname = ‘Paul’ OR Firstname = ‘Alan’ OR Firstname = ‘Tom’;
SELECT *
FROM EMP
WHERE Firstname IN (‘Peter’, ‘Steven’, ‘Paul’, ‘Alan’, ‘Tom’);
What would we do differently when writing an IN clause containing text rather than numbers? For example names rather than years.
We have to contain the text in quotation marks
Do we ever wrap text in double quotes “ ” in SQL?
No, we only use single quotes ‘ ’
How would we modify the IN clause to return all columns from a table called Staff that does not show the Accounts or Personnel dept? (from the dept column)
SELECT *
FROM Staff
WHERE dept NOT IN (‘Accounts’, ‘Personnel’);
Can you name another data type apart from Text that we have to enclose in quotation marks?
Date
What is the BETWEEN Operator used for?
The BETWEEN operator returns a range of data. This can be numbers, dates, text etc…
Write a query that returns all employees that receive a salary between 25000 and 40000
All columns to be returned
EMP Table
SELECT *
FROM EMP
WHERE Sal BETWEEN 25000 AND 40000;
Is the BETWEEN Operator Inclusive or non inclusive?
Inclusive
Eg BETWEEN 20 AND 40 would return matches for 20 and 40 as well as everything in between.
Write a query that will return all Department Numbers DEPTNO not between 20 and 30
All Columns to be returned
Table name DEPT
SELECT *
FROM Dept
Where DEPTNO NOT BETWEEN 20 AND 30;
Write query that would return all columns from the Dept table that have no data in the DEPTNO column
SELECT *
FROM Dept
WHERE DeptNo IS NULL;
Write a query that returns those employees that don’t make any commission and have a salary greater than 1100 but less than 5000. Exclude those employees that have a salary equal to 3000
In this table the employees that don’t receive commission are either don’t have anything written in the comm column or have a 0 written in the comm column. Your query has to accommodate this
SELECT * FROM EMP WHERE ( COMM IS NULL OR COMM = 0 ) AND SAL > 1100 AND SAL < 5000 AND SAL != 3000;
When writing the not equals to operator we use !=. What is an alternative way to write not equals to?
Less than greater than < >
The presence of parenthesis causes the conditions within them to be……?
Evaluated together