### 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)
 

MySQL online editor

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.

About MySQL

MySQL is a open-source, free and very popular relational database management system which is developed, distributed and supported by Oracle corporation.

Key Features:

  • Open-source relational database management systems.
  • Reliable, very fast and easy to use database server.
  • Works on client-server model.
  • Highly Secure and Scalable
  • High Performance
  • High productivity as it uses stored procedures, triggers, views to write a highly productive code.
  • Supports large databases efficiently.
  • Supports many operating systems like Linux*,CentOS*, Solaris*,Ubuntu*,Windows*, MacOS*,FreeBSD* and others.

Syntax help

Commands

1. CREATE

CREATE TABLE table_name (
                column1 datatype,
                column2 datatype,
                ....);

Example

CREATE TABLE EMPLOYEE (
  empId INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  dept TEXT NOT NULL
);

2. ALTER

ALTER TABLE Table_name ADD column_name datatype;

Example

INSERT INTO EMPLOYEE VALUES (0001, 'Dave', 'Sales');

3. TRUNCATE

TRUNCATE table table_name;

4. DROP

DROP TABLE table_name;

5. RENAME

RENAME TABLE table_name1 to new_table_name1; 

6. COMMENT

Single-Line Comments:

 --Line1;

Multi-Line comments:

   /* Line1,
   Line2 */

DML Commands

1. INSERT

INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);

Note: Column names are optional.

Example

INSERT INTO EMPLOYEE VALUES (0001, 'Ava', 'Sales');

2. SELECT

SELECT column1, column2, ...
FROM table_name
[where condition]; 

Example

SELECT * FROM EMPLOYEE where dept ='sales';

3. UPDATE

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition; 

Example

UPDATE EMPLOYEE SET dept = 'Sales' WHERE empId='0001'; 

4. DELETE

DELETE FROM table_name where condition;

Example

DELETE from EMPLOYEE where empId='0001'; 

Indexes

1. CREATE INDEX

  CREATE INDEX index_name on table_name(column_name);
  • To Create Unique index:
  CREATE UNIQUE INDEX index_name on table_name(column_name);

2. DROP INDEX

DROP INDEX index_name ON table_name;

Views

1. Create a View

Creating a View:
CREATE VIEW View_name AS 
Query;

2. How to call view

SELECT * FROM View_name;

3. Altering a View

ALTER View View_name AS 
Query;

4. Deleting a View

DROP VIEW View_name;

Triggers

1. Create a Trigger

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 } */

2. Drop a Trigger

DROP TRIGGER [IF EXISTS] trigger_name;

Stored Procedures

1. Create a Stored Procedure

CREATE PROCEDURE sp_name(p1 datatype)
BEGIN
/*Stored procedure code*/
END;

2. How to call Stored procedure

CALL sp_name;

3. How to delete stored procedure

DROP PROCEDURE sp_name;

Joins

1. INNER JOIN

SELECT * FROM TABLE1 INNER JOIN TABLE2 where condition;

2. LEFT JOIN

SELECT * FROM TABLE1 LEFT JOIN TABLE2 ON condition;

3. RIGHT JOIN

SELECT * FROM TABLE1 RIGHT JOIN TABLE2 ON condition;

4. CROSS JOIN

SELECT select_list from TABLE1 CROSS JOIN TABLE2;