SQL 2 Flashcards
What do we use the ; (semi colon) for in SQL?
The ; (Semi colon) is used to terminate a statement.
What statement would return all records from a Table called ‘Emp’ where the surname is Jones. We Want all columns returned.
SELECT *
FROM Emp
WHERE surname = ‘Jones’;
We want to return surname Jones and JobTitle is Musician. What statement will do this?
Table name is Emp
All Columns to be Returned
SELECT *
FROM Emp
WHERE surname = ‘Jones’
AND JobTitle = ‘Musician’;
What order would these words appear in an SQL statement? WHERE, FROM, AND SELECT
SELECT
FROM
WHERE
AND
From a table called EMP we want to return only the firstname, secondname, Sal and dept columns where surname is Jones and Sal is more than 60000
SELECT Firstname, secondname, sal, dept
FROM EMP
WHERE secondname = ‘Jones’
AND Sal > 60000;
STRINGS are wrapped in…,,,,,,?
Quotation Marks
Write a query that will return all employees that have a commission (Comm) greater than their Salary (Sal).
Tablename is EMP
All Columns to be returned
SELECT *
FROM Emp
WHERE comm > sal;
Write a query that returns all employees (Job) that are NOT managers, have a salary greater than 25000 and Deptno is 20.
Tablename is EMP
All rows to be returned
SELECT * FROM Emp WHERE job != 'Manager' AND sal > 25000 AND deptno = 20;
Write a query that returns all employees whose job title is either Clerk or salesman.
Tablename is Emp
All Columns to be returned
SELECT *
FROM Emp
WHERE JOB = ‘Clerk’
OR Job = ‘Salesman’;
Write a query that returns all employees that are not managers or Salesman and earn a salary greater than or equal to 25000
Tablename is Emp
We only want to see the name column
SELECT Name FROM Emp WHERE Job != 'Manager' AND Job != 'Salesman' AND Salary >= 25000;
Check out the below task and the solution. Why do we use ‘AND Job != ‘Salesman’ rather than ‘OR Job != ‘Salesman’?
Write a query that returns all employees that are not managers or Salesman and earn a salary greater than or equal to 25000
Tablename is Emp
We only want to see the name column
SELECT Name FROM Emp WHERE Job != 'Manager' AND Job != 'Salesman' AND Salary >= 25000;
We use AND rather than OR because by using OR we are cancelling out both clauses
not equal to manager
OR not equal to Salesman
Means that the result set will display all managers and all salesmen.
SQL can parse the command but it will not return what we want. Therefore AND is the only solution. This will cancel out both managers and salesmen.
What does Parse mean?
To break something down into parts in order to understand each element.
What is the difference between Data Definition language (DDL) and Data manipulation Language (DML)?
DDL (Data Definition Language) is used to create and modify the structure of a database.
DML (Data Manipulation Language) is used to access, modify or retrieve the data from the database.