### 1. Find the addresses (location_id, street_address, city, state_province, country_name) of all the departments SELECT location_id, street_address, city, state_province, country_name FROM locations JOIN departments ON locations.location_id = departments.location_id; ### 2. Find the name (first_name, last name), department ID and name of all the employees SELECT first_name, last_name, department_id, department_name FROM employees JOIN departments ON employees.department_id = departments.department_id; ### 3. Find the name (first_name, last_name), job, department ID and name of the employees who works in London SELECT first_name, last_name, job_title, department_id, department_name FROM employees JOIN departments ON employees.department_id = departments.department_id WHERE city = 'London'; ### 4. Write a query to find the employee id, name (last_name) along with their manager_id and name (last_name) SELECT employee_id, last_name, manager_id, manager_last_name FROM employees JOIN employees AS m ON employees.manager_id = m.employee_id; ### 5. Write a query to find the name (first_name, last_name) and hire date of the employees who was hired after 'Jones' SELECT first_name, last_name, hire_date FROM employees WHERE last_name > 'Jones'; ### 6. Write a query to get the department name and number of employees in the department SELECT department_name, COUNT(*) AS number_of_employees FROM departments JOIN employees ON departments.department_id = employees.department_id GROUP BY department_name; ### 7. Write a query to display department name, name (first_name, last_name), hire date, salary of the manager for all managers whose experience is more than 15 years SELECT department_name, first_name, last_name, hire_date, salary FROM employees JOIN departments ON employees.department_id = departments.department_id WHERE manager_id IS NOT NULL AND TIMESTAMPDIFF(YEAR, hire_date, CURRENT_DATE) > 15; ### 8. Write a query to find the name (first_name, last_name) and the salary of the employees who have a higher salary than the employee whose last_name='Bull' SELECT first_name, last_name, salary FROM employees WHERE salary > (SELECT salary FROM employees WHERE last_name = 'Bull'); ### 9. Write a query to find the name (first_name, last_name) of all employees who works in the IT department SELECT first_name, last_name FROM employees JOIN departments ON employees.department_id = departments.department_id WHERE department_name = 'IT'; ### 10. Write a query to find the name (first_name, last_name) of the employees who have a manager and worked in a USA based department SELECT first_name, last_name FROM employees JOIN departments ON employees.department_id = departments.department_id JOIN locations ON departments.location_id = locations.location_id WHERE manager_id IS NOT NULL AND country_name = 'USA'; ### 11. Write a query to find the name (first_name, last_name), and salary of the employees whose salary is greater than the average salary SELECT first_name, last_name, salary FROM employees WHERE salary > (SELECT AVG(salary) FROM employees); ### 12. Write a query to find the name (first_name, last_name), and salary of the employees whose salary is equal to the minimum salary for their job grade SELECT first_name, last_name, salary FROM employees JOIN jobs ON employees.job_id = jobs.job_id WHERE salary = jobs.minimum_salary; ### 13. Write a query to find the name (first_name, last_name), and salary of the employees who earns more than the average salary and works in any of the IT departments SELECT first_name, last_name, salary FROM employees JOIN departments ON employees.department_id = departments.department_id WHERE salary > (SELECT AVG(salary) FROM employees)
Write, Run & Share MySQL queries online using OneCompiler's MySQL online editor and compiler for free. It's one of the robust, feature-rich online editor and compiler for MySQL. Getting started with the OneCompiler's MySQL editor is really simple and pretty fast. The editor shows sample boilerplate code when you choose language as 'MySQL' and start writing queries to learn and test online without worrying about tedious process of installation.
MySQL is a open-source, free and very popular relational database management system which is developed, distributed and supported by Oracle corporation.
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
....);
CREATE TABLE EMPLOYEE (
empId INTEGER PRIMARY KEY,
name TEXT NOT NULL,
dept TEXT NOT NULL
);
ALTER TABLE Table_name ADD column_name datatype;
INSERT INTO EMPLOYEE VALUES (0001, 'Dave', 'Sales');
TRUNCATE table table_name;
DROP TABLE table_name;
RENAME TABLE table_name1 to new_table_name1;
--Line1;
/* Line1,
Line2 */
INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);
Note: Column names are optional.
INSERT INTO EMPLOYEE VALUES (0001, 'Ava', 'Sales');
SELECT column1, column2, ...
FROM table_name
[where condition];
SELECT * FROM EMPLOYEE where dept ='sales';
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
UPDATE EMPLOYEE SET dept = 'Sales' WHERE empId='0001';
DELETE FROM table_name where condition;
DELETE from EMPLOYEE where empId='0001';
CREATE INDEX index_name on table_name(column_name);
CREATE UNIQUE INDEX index_name on table_name(column_name);
DROP INDEX index_name ON table_name;
Creating a View:
CREATE VIEW View_name AS
Query;
SELECT * FROM View_name;
ALTER View View_name AS
Query;
DROP VIEW View_name;
CREATE TRIGGER trigger_name trigger_time trigger_event
ON tbl_name FOR EACH ROW [trigger_order] trigger_body
/* where
trigger_time: { BEFORE | AFTER }
trigger_event: { INSERT | UPDATE | DELETE }
trigger_order: { FOLLOWS | PRECEDES } */
DROP TRIGGER [IF EXISTS] trigger_name;
CREATE PROCEDURE sp_name(p1 datatype)
BEGIN
/*Stored procedure code*/
END;
CALL sp_name;
DROP PROCEDURE sp_name;
SELECT * FROM TABLE1 INNER JOIN TABLE2 where condition;
SELECT * FROM TABLE1 LEFT JOIN TABLE2 ON condition;
SELECT * FROM TABLE1 RIGHT JOIN TABLE2 ON condition;
SELECT select_list from TABLE1 CROSS JOIN TABLE2;