Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
100% found this document useful (1 vote)
166 views

45677777MySQL - All Basics Query by Using PopSQL Text Editor

The document contains SQL statements to create tables for an employee database including tables for employees, branches, clients, branch suppliers, and works_with. It then populates the tables with sample data for employees in different branches, clients, branch suppliers, and sales amounts. It also includes several SELECT statements to query the data, such as finding employees by name, branch, gender, birthdate range or other attributes and performing aggregation, sorting, limiting, unions and wildcard searches on the data.

Uploaded by

Mahadevamma S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
166 views

45677777MySQL - All Basics Query by Using PopSQL Text Editor

The document contains SQL statements to create tables for an employee database including tables for employees, branches, clients, branch suppliers, and works_with. It then populates the tables with sample data for employees in different branches, clients, branch suppliers, and sales amounts. It also includes several SELECT statements to query the data, such as finding employees by name, branch, gender, birthdate range or other attributes and performing aggregation, sorting, limiting, unions and wildcard searches on the data.

Uploaded by

Mahadevamma S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

MySQL 1

MySQL 2

---making the database


CREATE TABLE employee (
emp_id INT PRIMARY KEY,
first_name VARCHAR(40),
last_name VARCHAR(40),
birth_day DATE,
sex VARCHAR(1),
salary INT,
super_id INT,
branch_id INT
);

CREATE TABLE branch (


branch_id INT PRIMARY KEY,
branch_name VARCHAR(40),
mgr_id INT,
mgr_start_date DATE,
FOREIGN KEY(mgr_id) REFERENCES employee(emp_id) ON DELET
E SET NULL
);

ALTER TABLE employee


ADD FOREIGN KEY(branch_id)
REFERENCES branch(branch_id)
ON DELETE SET NULL;

ALTER TABLE employee


ADD FOREIGN KEY(super_id)
REFERENCES employee(emp_id)
ON DELETE SET NULL;

CREATE TABLE client (


client_id INT PRIMARY KEY,
client_name VARCHAR(40),
branch_id INT,
MySQL 3

FOREIGN KEY(branch_id) REFERENCES branch(branch_id) ON DELET


E SET NULL
);

CREATE TABLE works_with (


emp_id INT,
client_id INT,
total_sales INT,
PRIMARY KEY(emp_id, client_id),
FOREIGN KEY(emp_id) REFERENCES employee(emp_id)
ON DELETE CASCADE,
FOREIGN KEY(client_id) REFERENCES client(client_id) ON DELET
E CASCADE
);

CREATE TABLE branch_supplier (


branch_id INT,
supplier_name VARCHAR(40),
supply_type VARCHAR(40),
PRIMARY KEY(branch_id, supplier_name),
FOREIGN KEY(branch_id) REFERENCES branch(branch_id) ON DELET
E CASCADE
);
-- Corporate
INSERT INTO employee VALUES(100, 'David', 'Wallace', '1967-11-
17', 'M', 250000, NULL, NULL);

INSERT INTO branch VALUES(1, 'Corporate', 100, '2006-02-09');

UPDATE employee
SET branch_id = 1
WHERE emp_id = 100;

INSERT INTO employee VALUES(101, 'Jan', 'Levinson', '1961-05-


11', 'F', 110000, 100, 1);
MySQL 4

-- Scranton
INSERT INTO employee VALUES(102, 'Michael', 'Scott', '1964-03-
15', 'M', 75000, 100, NULL);

INSERT INTO branch VALUES(2, 'Scranton', 102, '1992-04-06');

UPDATE employee
SET branch_id = 2
WHERE emp_id = 102;

INSERT INTO employee VALUES(103, 'Angela', 'Martin', '1971-06-


25', 'F', 63000, 102, 2);
INSERT INTO employee VALUES(104, 'Kelly', 'Kapoor', '1980-02-
05', 'F', 55000, 102, 2);
INSERT INTO employee VALUES(105, 'Stanley', 'Hudson', '1958-02-
19', 'M', 69000, 102, 2);

-- Stamford
INSERT INTO employee VALUES(106, 'Josh', 'Porter', '1969-09-
05', 'M', 78000, 100, NULL);

INSERT INTO branch VALUES(3, 'Stamford', 106, '1998-02-13');

UPDATE employee
SET branch_id = 3
WHERE emp_id = 106;

INSERT INTO employee VALUES(107, 'Andy', 'Bernard', '1973-07-


22', 'M', 65000, 106, 3);
INSERT INTO employee VALUES(108, 'Jim', 'Halpert', '1978-10-
01', 'M', 71000, 106, 3);

-- BRANCH SUPPLIER
INSERT INTO branch_supplier VALUES(2, 'Hammer Mill', 'Paper');
MySQL 5

INSERT INTO branch_supplier VALUES(2, 'Uni-


ball', 'Writing Utensils');
INSERT INTO branch_supplier VALUES(3, 'Patriot Paper', 'Paper')
;
INSERT INTO branch_supplier VALUES(2, 'J.T. Forms & Labels', 'C
ustom Forms');
INSERT INTO branch_supplier VALUES(3, 'Uni-
ball', 'Writing Utensils');
INSERT INTO branch_supplier VALUES(3, 'Hammer Mill', 'Paper');
INSERT INTO branch_supplier VALUES(3, 'Stamford Lables', 'Custo
m Forms');

-- CLIENT
INSERT INTO client VALUES(400, 'Dunmore Highschool', 2);
INSERT INTO client VALUES(401, 'Lackawana Country', 2);
INSERT INTO client VALUES(402, 'FedEx', 3);
INSERT INTO client VALUES(403, 'John Daly Law, LLC', 3);
INSERT INTO client VALUES(404, 'Scranton Whitepages', 2);
INSERT INTO client VALUES(405, 'Times Newspaper', 3);
INSERT INTO client VALUES(406, 'FedEx', 2);

-- WORKS_WITH
INSERT INTO works_with VALUES(105, 400, 55000);
INSERT INTO works_with VALUES(102, 401, 267000);
INSERT INTO works_with VALUES(108, 402, 22500);
INSERT INTO works_with VALUES(107, 403, 5000);
INSERT INTO works_with VALUES(108, 403, 12000);
INSERT INTO works_with VALUES(105, 404, 33000);
INSERT INTO works_with VALUES(107, 405, 26000);
INSERT INTO works_with VALUES(102, 406, 15000);
INSERT INTO works_with VALUES(105, 406, 130000);
MySQL 6

-- Find all employees


SELECT *
FROM employee;

-- Find all clients


SELECT *
FROM client;
MySQL 7

-- Find all employees ordered by salary


SELECT *
from employee
ORDER BY salary DESC;

-- Find all employees ordered by sex then name


SELECT *
from employee
ORDER BY sex, first_name, last_name;
MySQL 8

-- Find the first 5 employees in the table


SELECT *
from employee
LIMIT 5;

-- Find the first and last names of all employees


SELECT first_name, employee.last_name
FROM employee;
MySQL 9

-- Find the forename and surnames names of all employees


SELECT first_name AS forename, employee.last_name AS surname
FROM employee;

-- Find out all the different genders


SELECT DISTINCT sex
FROM employee;

-- Find all male employees


SELECT *
FROM employee
WHERE sex = 'M';

-- Find all employees at branch 2


SELECT *
FROM employee
WHERE branch_id = 2;
MySQL 10

-- Find all employee's id's and names who were born after 1969
SELECT emp_id, first_name, last_name
FROM employee
WHERE birth_day >= 1970-01-01;

-- Find all female employees at branch 2


SELECT *
FROM employee
WHERE branch_id = 2 AND sex = 'F';

--
Find all employees who are female & born after 1969 or who make
over 80000
SELECT *
FROM employee
WHERE (birth_day >= '1970-01-
01' AND sex = 'F') OR salary > 80000;
MySQL 11

-- Find all employees born between 1970 and 1975


SELECT *
FROM employee
WHERE birth_day BETWEEN '1970-01-01' AND '1975-01-01';

-- Find all employees named Jim, Michael, Johnny or David


SELECT *
FROM employee
WHERE first_name IN ('Jim', 'Michael', 'Johnny', 'David');

-- Find the number of employees


SELECT COUNT(super_id)
FROM employee;
Ans : 8

-- Find the average of all employee's salaries


SELECT AVG(salary)
FROM employee;
Ans: 92888.8889

-- Find the sum of all employee's salaries


SELECT SUM(salary)
FROM employee;
Ans: 836000
MySQL 12

-- Find out how many males and females there are


SELECT COUNT(sex), sex
FROM employee
GROUP BY sex;
Ans: M=6 , F = 3

-- Find the total sales of each salesman


SELECT SUM(total_sales), emp_id
FROM works_with
GROUP BY client_id;

-- Find the total amount of money spent by each client


SELECT SUM(total_sales), client_id
FROM works_with
GROUP BY client_id;
MySQL 13

-- % = any # characters, _ = one character

-- Find any client's who are an LLC


SELECT *
FROM client
WHERE client_name LIKE '%LLC';

-- Find any branch suppliers who are in the label business


SELECT *
FROM branch_supplier
WHERE supplier_name LIKE '% Label%';

-- Find any employee born on the 10th day of the month


SELECT *
FROM employee
WHERE birth_day LIKE '____-02%';

-- Find any clients who are schools


SELECT *
FROM client
WHERE client_name LIKE '%Highschool%';
MySQL 14

-- Find a list of employee and branch names


SELECT employee.first_name AS Employee_Branch_Names
FROM employee
UNION
SELECT branch.branch_name
FROM branch;
MySQL 15

-- Find a list of all clients & branch suppliers' names


SELECT client_name , branch_id
FROM client
UNION
SELECT supplier_name,branch_id
FROM branch_supplier;
MySQL 16

--Find a list of all money spent or earned by the company


SELECT salary
FROM employee
UNION
SELECT total_sales
FROM works_with;
MySQL 17
MySQL 18
MySQL 19
MySQL 20
MySQL 21
MySQL 22

**The shown example taken from internet.

**This note prepared by C S Ponkoj

**Used PopSQL text editor

You might also like