SQL 3 Flashcards

1
Q

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.

A

SELECT name, dept
FROM EMP
WHERE startdate IN (2017, 2019);

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

The IN Clause reduces the amount of text in a query by replacing what clause?

A

The OR Clause

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

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’;
A

SELECT *
FROM EMP
WHERE Firstname IN (‘Peter’, ‘Steven’, ‘Paul’, ‘Alan’, ‘Tom’);

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

What would we do differently when writing an IN clause containing text rather than numbers? For example names rather than years.

A

We have to contain the text in quotation marks

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

Do we ever wrap text in double quotes “ ” in SQL?

A

No, we only use single quotes ‘ ’

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

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)

A

SELECT *
FROM Staff
WHERE dept NOT IN (‘Accounts’, ‘Personnel’);

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

Can you name another data type apart from Text that we have to enclose in quotation marks?

A

Date

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

What is the BETWEEN Operator used for?

A

The BETWEEN operator returns a range of data. This can be numbers, dates, text etc…

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

Write a query that returns all employees that receive a salary between 25000 and 40000

All columns to be returned
EMP Table

A

SELECT *
FROM EMP
WHERE Sal BETWEEN 25000 AND 40000;

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

Is the BETWEEN Operator Inclusive or non inclusive?

A

Inclusive

Eg BETWEEN 20 AND 40 would return matches for 20 and 40 as well as everything in between.

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

Write a query that will return all Department Numbers DEPTNO not between 20 and 30

All Columns to be returned
Table name DEPT

A

SELECT *
FROM Dept
Where DEPTNO NOT BETWEEN 20 AND 30;

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

Write query that would return all columns from the Dept table that have no data in the DEPTNO column

A

SELECT *
FROM Dept
WHERE DeptNo IS NULL;

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

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

A
SELECT *
FROM EMP
WHERE ( COMM IS NULL OR COMM = 0 )
AND SAL > 1100 
AND SAL < 5000
AND SAL != 3000;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

When writing the not equals to operator we use !=. What is an alternative way to write not equals to?

A

Less than greater than < >

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

The presence of parenthesis causes the conditions within them to be……?

A

Evaluated together

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

What do we do with ANDs and ORs?

A

Group them together. Maybe even encapsulate them in parenthesis in order to ensure they are evaluated together.

17
Q

If you have a lot of AND conditions in SQL how should you write them?

A

They should all be combined together

Eg
CORRECT
AND SAL > 1100 AND SAL < 5000
AND SAL != 3000
OR COMM = 0;

NOT CORRECT
AND SAL > 1100 AND SAL < 5000
OR COMM = 0
AND SAL != 3000;