SQL SELECT statement
SELECT FROM
Select the information from the “dept_no” column of the “departments” table.
Select all data from the “departments” table.
SELECT
dept_no
FROM
departments;
SELECT
*
FROM
departments;
WHERE clause
It will allow us to set a condition upon which we will specify what part of the data we want to retrieve from the DB.
Select all people from the “employees” table whose first name is “Elvis”. 😊
SELECT
*
FROM
employees
WHERE
first_name = 'Elvis';
AND operator
AND : Allow you to logically combine two statements in the condition code block. Allow us to narrow the output we would like to extract from our data.
Retrieve a list with all female employees whose first name is Kellie.
SELECT
*
FROM
employees
WHERE
first_name = 'Kellie' AND gender = 'F';
OR Operator
Retrieve a list with all employees whose first name is either Kellie or Aruna.
SELECT
*
FROM
employees
WHERE
gender = 'F' AND (first_name = 'Kellie' OR first_name = 'Aruna');
IN - NOT IN Operator
IN : The IN operator allows SQL to return the names written in parentheses, if they exists in our table.
NOT IN : This is the exact opposite to the IN Operator.
Use the IN operator to select all individuals from the “employees” table, whose first name is either “Denis”, or “Elvis”.
SELECT
*
FROM
employees
WHERE
first_name IN ('Denis' , 'Elvis');
SELECT
*
FROM
employees
WHERE
first_name NOT IN ('John' , 'Mark', 'Jacob');
Working with the “employees” table, use the LIKE operator to select the data about all individuals, whose first name starts with “Mark”; specify that the name can be succeeded by any sequence of characters.
Retrieve a list with all employees who have been hired in the year 2000.
Retrieve a list with all employees whose employee number is written with 5 characters, and starts with “1000”.
Extract all individuals from the ‘employees’ table whose first name contains “Jack”.
Once you have done that, extract another list containing the names of employees that do not contain “Jack”.
SELECT
*
FROM
employees
WHERE
first_name LIKE ('%JACK%');
SELECT
*
FROM
employees
WHERE
first_name NOT LIKE ('%Jack%');
BETWEEN...AND...
This help us to designate the interval to which a given value belongs.
Select all the information from the “salaries” table regarding contracts from 66,000 to 70,000 dollars per year.
Retrieve a list with all individuals whose employee number is not between ‘10004’ and ‘10012’.
Select the names of all departments with numbers between ‘d003’ and ‘d006’.
SELECT
*
FROM
salaries;
SELECT
*
FROM
salaries
WHERE
salary BETWEEN 66000 AND 70000
;
SELECT
*
FROM
employees
WHERE
emp_no NOT BETWEEN '10004' AND '10012'
;
SELECT
dept_name
FROM
departments
WHERE
dept_no BETWEEN 'd003' AND 'd006';
IS NOT NULL / IS NULL
Select the names of all departments whose department number value is not null.
Retrieve a list with data about all female employees who were hired in the year 2000 or after.
Hint: If you solve the task correctly, SQL should return 7 rows.
Extract a list with all employees’ salaries higher than $150,000 per annum.
SELECT
*
FROM
employees
WHERE
hire_date >= '2000-01-01'
AND gender = 'F';
SELECT
*
FROM
salaries
WHERE
salary > 150000;
SELECT DISTINCT
Obtain a list with all different “hire dates” from the “employees” table.
Expand this list and click on “Limit to 1000 rows”. This way you will set the limit of output rows displayed back to the default of 1000.
In the next lecture, we will show you how to manipulate the limit rows count.
SELECT DISTINCT
hire_date
FROM
employees;
Introduction to aggregate functions
They are applied on multiple rows of the single column of a table and return an output of a single value.
They ignore NULL values unless told not to.
COUNT : 1:) select count(emp_no) from employee;
2:) select count(distinct first_name) from employee;
How many annual contracts with a value higher than or equal to $100,000 have been registered in the salaries table?
How many managers do we have in the “employees” database? Use the star symbol (*) in your code to solve this exercise.
SELECT
COUNT(*)
FROM
salaries
WHERE
salary >= 100000;
SELECT
COUNT(*)
FROM
dept_manager;
ORDER BY
We can order output by using order by clause.To set the order by according to multiple columns we have to place the column names side by side separated with comma.
Select all data from the “employees” table, ordering it by “hire date” in descending order.
SELECT
*
FROM
employees
ORDER BY hire_date DESC;
GROUP BY
By grouping the query - 1:) Distinct value will be selected.
2:) In most cases , when you need an aggregate function , you must add a Group by clause in your query too.
3:) It is same same as the select distinct but , Group by is slower then the select distinct.
assume we need to column layout with name and the number of time that name occurs then :
4:) Always include the field you have grouped your results by in the SELECT STATEMENT.
practice :
#group by clause
SELECT
first_name
FROM
employees;
#group by
SELECT
first_name
FROM
employees
GROUP BY first_name;
#counting with group by
SELECT
COUNT(first_name)
FROM
employees
GROUP BY first_name;
#getting the name and their total occurances
SELECT
first_name, COUNT(first_name)
FROM
employees
GROUP BY first_name;
#placing the order by clause too
SELECT
first_name, COUNT(first_name)
FROM
employees
GROUP BY first_name
ORDER BY first_name DESC;
Using Aliases (AS)
Used to rename a selection from your query
This will be a slightly more sophisticated task.
Write a query that obtains two columns. The first column must contain annual salaries higher than 80,000 dollars. The second column, renamed to “emps_with_same_salary”, must show the number of employees contracted to that salary. Lastly, sort the output by the first column.
Select all employees whose average salary is higher than $120,000 per annum.
Hint: You should obtain 101 records.
Compare the output you obtained with the output of the following two queries:
SELECT
*, AVG(salary)
FROM
salaries
WHERE
salary > 120000
GROUP BY emp_no
ORDER BY emp_no;
SELECT
*, AVG(salary)
FROM
salaries
GROUP BY emp_no
HAVING AVG(salary) > 120000;
SELECT
emp_no, AVG(salary)
FROM
salaries
GROUP BY emp_no
HAVING AVG(salary) > 120000
ORDER BY emp_no;
When using WHERE instead of HAVING, the output is larger because in the output we include individual contracts higher than $120,000 per year. The output does not contain average salary values.
Finally, using the star symbol instead of “emp_no” extracts a list that contains all columns from the “salaries” table.
WHERE vs HAVING- Part I
WHERE vs HAVING- Part II
Select the employee numbers of all individuals who have signed more than 1 contract after the 1st of January 2000.
Hint: To solve this exercise, use the “dept_emp” table.
Select the first 100 rows from the ‘dept_emp’ table.
SELECT
*
FROM
dept_emp
LIMIT 100;
Comments
Post a Comment