Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
44 views6 pages

DBML 3

Uploaded by

wenep10941
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views6 pages

DBML 3

Uploaded by

wenep10941
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Enter password: ******

Welcome to the MySQL monitor. Commands end with ; or \g.


Your MySQL connection id is 10
Server version: 8.0.39 MySQL Community Server - GPL

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its


affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> CREATE DATABASE dbmlthree;


Query OK, 1 row affected (0.03 sec)

mysql> USE DATABASE dbmlthree;


ERROR 1049 (42000): Unknown database 'database'
mysql> USE dbmlthree;
Database changed
mysql> CREATE TABLE Departments (
-> DepartmentID INT PRIMARY KEY,
-> DepartmentName VARCHAR(50)
-> );
Query OK, 0 rows affected (0.06 sec)

mysql> CREATE TABLE Employees (


-> EmployeeID INT PRIMARY KEY,
-> FirstName VARCHAR(50),
-> LastName VARCHAR(50),
-> DepartmentID INT,
-> ManagerID INT,
-> DateHired DATE,
-> FOREIGN KEY (DepartmentID) REFERENCES Departments(DepartmentID),
-> FOREIGN KEY (ManagerID) REFERENCES Employees(EmployeeID)
-> );
Query OK, 0 rows affected (0.04 sec)

mysql>
mysql> CREATE TABLE Salaries (
-> EmployeeID INT,
-> Salary DECIMAL(10, 2),
-> EffectiveDate DATE,
-> PRIMARY KEY (EmployeeID, EffectiveDate),
-> FOREIGN KEY (EmployeeID) REFERENCES Employees(EmployeeID)
-> );
Query OK, 0 rows affected (0.04 sec)

mysql> -- Insert Departments


mysql> INSERT INTO Departments (DepartmentID, DepartmentName) VALUES
-> (1, 'HR'),
-> (2, 'Finance'),
-> (3, 'Engineering');
Query OK, 3 rows affected (0.03 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql>
mysql> -- Insert Employees
mysql> INSERT INTO Employees (EmployeeID, FirstName, LastName, DepartmentID,
ManagerID, DateHired) VALUES
-> (1, 'John', 'Doe', 1, NULL, '2015-06-15'),
-> (2, 'Jane', 'Smith', 2, 1, '2017-03-12'),
-> (3, 'Mike', 'Johnson', 3, 1, '2018-09-23'),
-> (4, 'Emily', 'Davis', 3, 3, '2019-11-02'),
-> (5, 'Chris', 'Brown', 2, 2, '2020-05-10');
Query OK, 5 rows affected (0.01 sec)
Records: 5 Duplicates: 0 Warnings: 0

mysql>
mysql> -- Insert Salaries
mysql> INSERT INTO Salaries (EmployeeID, Salary, EffectiveDate) VALUES
-> (1, 60000.00, '2015-06-15'),
-> (2, 55000.00, '2017-03-12'),
-> (3, 75000.00, '2018-09-23'),
-> (4, 50000.00, '2019-11-02'),
-> (5, 52000.00, '2020-05-10');
Query OK, 5 rows affected (0.01 sec)
Records: 5 Duplicates: 0 Warnings: 0

mysql> SELECT * FROM Employees;


+------------+-----------+----------+--------------+-----------+------------+
| EmployeeID | FirstName | LastName | DepartmentID | ManagerID | DateHired |
+------------+-----------+----------+--------------+-----------+------------+
| 1 | John | Doe | 1 | NULL | 2015-06-15 |
| 2 | Jane | Smith | 2 | 1 | 2017-03-12 |
| 3 | Mike | Johnson | 3 | 1 | 2018-09-23 |
| 4 | Emily | Davis | 3 | 3 | 2019-11-02 |
| 5 | Chris | Brown | 2 | 2 | 2020-05-10 |
+------------+-----------+----------+--------------+-----------+------------+
5 rows in set (0.02 sec)

mysql> SELECT * FROM Departments;


+--------------+----------------+
| DepartmentID | DepartmentName |
+--------------+----------------+
| 1 | HR |
| 2 | Finance |
| 3 | Engineering |
+--------------+----------------+
3 rows in set (0.00 sec)
mysql> SELECT * FROM Salaries;
+------------+----------+---------------+
| EmployeeID | Salary | EffectiveDate |
+------------+----------+---------------+
| 1 | 60000.00 | 2015-06-15 |
| 2 | 55000.00 | 2017-03-12 |
| 3 | 75000.00 | 2018-09-23 |
| 4 | 50000.00 | 2019-11-02 |
| 5 | 52000.00 | 2020-05-10 |
+------------+----------+---------------+
5 rows in set (0.00 sec)

mysql> SELECT e.EmployeeID, e.FirstName, e.LastName, d.DepartmentName


-> FROM Employees e
-> INNER JOIN Departments d ON e.DepartmentID = d.DepartmentID;
+------------+-----------+----------+----------------+
| EmployeeID | FirstName | LastName | DepartmentName |
+------------+-----------+----------+----------------+
| 1 | John | Doe | HR |
| 2 | Jane | Smith | Finance |
| 5 | Chris | Brown | Finance |
| 3 | Mike | Johnson | Engineering |
| 4 | Emily | Davis | Engineering |
+------------+-----------+----------+----------------+
5 rows in set (0.01 sec)

mysql> SELECT d.DepartmentName, e.FirstName, e.LastName


-> FROM Departments d
-> LEFT JOIN Employees e ON d.DepartmentID = e.DepartmentID;
+----------------+-----------+----------+
| DepartmentName | FirstName | LastName |
+----------------+-----------+----------+
| HR | John | Doe |
| Finance | Jane | Smith |
| Finance | Chris | Brown |
| Engineering | Mike | Johnson |
| Engineering | Emily | Davis |
+----------------+-----------+----------+
5 rows in set (0.00 sec)

mysql> SELECT e.FirstName AS EmployeeName, m.FirstName AS ManagerName


-> FROM Employees e
-> RIGHT JOIN Employees m ON e.ManagerID = m.EmployeeID;
+--------------+-------------+
| EmployeeName | ManagerName |
+--------------+-------------+
| Jane | John |
| Mike | John |
| Chris | Jane |
| Emily | Mike |
| NULL | Emily |
| NULL | Chris |
+--------------+-------------+
6 rows in set (0.01 sec)

mysql> SELECT e.EmployeeID, e.FirstName, e.LastName, d.DepartmentName


-> FROM Employees e
-> FULL OUTER JOIN Departments d ON e.DepartmentID = d.DepartmentID;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'FULL
OUTER JOIN Departments d ON e.DepartmentID = d.DepartmentID' at line 3
mysql> SELECT e.EmployeeID, e.FirstName, e.LastName, d.DepartmentName
-> FROM Employees e
-> FULL OUTER JOIN Departments d ON e.DepartmentID = d.DepartmentID;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'FULL
OUTER JOIN Departments d ON e.DepartmentID = d.DepartmentID' at line 3
mysql> SELECT e.EmployeeID, e.FirstName, e.LastName, d.DepartmentName
-> FROM Employees e
-> FULL OUTER JOIN Departments d ON e.DepartmentID = d.DepartmentID;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'FULL
OUTER JOIN Departments d ON e.DepartmentID = d.DepartmentID' at line 3
mysql> SELECT * FROM Salaries
-> WHERE Salary = (SELECT MAX(Salary) FROM Salaries);
+------------+----------+---------------+
| EmployeeID | Salary | EffectiveDate |
+------------+----------+---------------+
| 3 | 75000.00 | 2018-09-23 |
+------------+----------+---------------+
1 row in set (0.00 sec)

mysql> SELECT e.FirstName, e.LastName, s.Salary


-> FROM Employees e
-> INNER JOIN Salaries s ON e.EmployeeID = s.EmployeeID
-> WHERE s.Salary > (SELECT AVG(Salary) FROM Salaries);
+-----------+----------+----------+
| FirstName | LastName | Salary |
+-----------+----------+----------+
| John | Doe | 60000.00 |
| Mike | Johnson | 75000.00 |
+-----------+----------+----------+
2 rows in set (0.00 sec)

mysql> SELECT e.FirstName, e.LastName, s.Salary


-> FROM Employees e
-> INNER JOIN Salaries s ON e.EmployeeID = s.EmployeeID
-> WHERE s.Salary > (SELECT AVG(Salary) FROM Salaries);
+-----------+----------+----------+
| FirstName | LastName | Salary |
+-----------+----------+----------+
| John | Doe | 60000.00 |
| Mike | Johnson | 75000.00 |
+-----------+----------+----------+
2 rows in set (0.00 sec)

mysql> CREATE VIEW EmployeeDetails AS


-> SELECT e.EmployeeID, e.FirstName, e.LastName, d.DepartmentName, s.Salary
-> FROM Employees e
-> INNER JOIN Departments d ON e.DepartmentID = d.DepartmentID
-> INNER JOIN Salaries s ON e.EmployeeID = s.EmployeeID;
Query OK, 0 rows affected (0.02 sec)

mysql> CREATE VIEW EmployeeDetails AS


-> SELECT e.EmployeeID, e.FirstName, e.LastName, d.DepartmentName, s.Salary
-> FROM Employees e
-> INNER JOIN Departments d ON e.DepartmentID = d.DepartmentID
-> INNER JOIN Salaries s ON e.EmployeeID = s.EmployeeID;
ERROR 1050 (42S01): Table 'EmployeeDetails' already exists
mysql> CREATE VIEW EmployeeDetails AS
-> SELECT e.EmployeeID, e.FirstName, e.LastName, d.DepartmentName, s.Salary
-> FROM Employees e
-> INNER JOIN Departments d ON e.DepartmentID = d.DepartmentID
-> INNER JOIN Salaries s ON e.EmployeeID = s.EmployeeID;
ERROR 1050 (42S01): Table 'EmployeeDetails' already exists
mysql> SELECT * FROM EmployeeDetails;
+------------+-----------+----------+----------------+----------+
| EmployeeID | FirstName | LastName | DepartmentName | Salary |
+------------+-----------+----------+----------------+----------+
| 1 | John | Doe | HR | 60000.00 |
| 2 | Jane | Smith | Finance | 55000.00 |
| 5 | Chris | Brown | Finance | 52000.00 |
| 3 | Mike | Johnson | Engineering | 75000.00 |
| 4 | Emily | Davis | Engineering | 50000.00 |
+------------+-----------+----------+----------------+----------+
5 rows in set (0.00 sec)

mysql> SELECT e1.FirstName AS Employee1, e2.FirstName AS Employee2, m.FirstName AS


Manager
-> FROM Employees e1
-> INNER JOIN Employees e2 ON e1.ManagerID = e2.ManagerID AND e1.EmployeeID <>
e2.EmployeeID
-> INNER JOIN Employees m ON e1.ManagerID = m.EmployeeID;
+-----------+-----------+---------+
| Employee1 | Employee2 | Manager |
+-----------+-----------+---------+
| Jane | Mike | John |
| Mike | Jane | John |
+-----------+-----------+---------+
2 rows in set (0.00 sec)

mysql> SELECT e.FirstName, e.LastName


-> FROM Employees e
-> WHERE EXISTS (
-> SELECT 1 FROM Departments d
-> WHERE e.DepartmentID = d.DepartmentID
-> AND d.DepartmentName = 'Engineering'
-> );
+-----------+----------+
| FirstName | LastName |
+-----------+----------+
| Mike | Johnson |
| Emily | Davis |
+-----------+----------+
2 rows in set (0.01 sec)

You might also like