NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

Here are the SQL commands to create the tables, insert records, and fulfill the requested queries.
1. Create Tables and Insert Data 🛠️
Create Tables
These commands define the structure for the DEPARTMENT and EMPLOYEE tables, including primary keys and the foreign key relationship.
-- Table 2: DEPARTMENT
CREATE TABLE DEPARTMENT (
DeptID INT PRIMARY KEY,
DeptName VARCHAR(50) NOT NULL,
Location VARCHAR(30)
);

-- Table 1: EMPLOYEE
CREATE TABLE EMPLOYEE (
EmpID INT PRIMARY KEY,
EmpName VARCHAR(50) NOT NULL,
DeptID INT,
Salary INT,
FOREIGN KEY (DeptID) REFERENCES DEPARTMENT(DeptID)
);

Insert Records
These commands populate both tables with at least five sample records.
-- Insert records into DEPARTMENT
INSERT INTO DEPARTMENT (DeptID, DeptName, Location) VALUES
(10, 'IT', 'Delhi'),
(20, 'HR', 'Noida'),
(30, 'Sales', 'Mumbai'),
(40, 'Finance', 'Delhi'),
(50, 'Marketing', 'Bangalore');

-- Insert records into EMPLOYEE
INSERT INTO EMPLOYEE (EmpID, EmpName, DeptID, Salary) VALUES
(101, 'Alice Johnson', 10, 60000),
(102, 'Bob Williams', 20, 45000),
(103, 'Charlie Brown', 10, 75000),
(104, 'David Lee', 30, 50000),
(105, 'Eve Davis', 40, 90000),
(106, 'Frank Miller', 20, 48000); -- Added an extra record for better query results

2. SQL Queries 📋
Display Employees in 'IT' Department (Equi Join)
This query uses an Equi Join (an INNER JOIN with an equality condition) to link the tables based on DeptID and filters the result by the department name.
SELECT
E.EmpName,
E.Salary,
D.DeptName
FROM
EMPLOYEE E
INNER JOIN
DEPARTMENT D ON E.DeptID = D.DeptID
WHERE
D.DeptName = 'IT';

Display Employees Located in 'Delhi'
This query joins the tables and filters the results based on the Location column in the DEPARTMENT table.
SELECT
E.EmpName,
D.DeptName,
D.Location
FROM
EMPLOYEE E
INNER JOIN
DEPARTMENT D ON E.DeptID = D.DeptID
WHERE
D.Location = 'Delhi';

Total Number of Employees in Each Department (GROUP BY)
This query uses the aggregate function COUNT() and the GROUP BY clause to count employees for each unique DeptName.
SELECT
D.DeptName,
COUNT(E.EmpID) AS TotalEmployees
FROM
EMPLOYEE E
INNER JOIN
DEPARTMENT D ON E.DeptID = D.DeptID
GROUP BY
D.DeptName;

Display All Employees Sorted by Salary (Descending)
This query retrieves all columns from the EMPLOYEE table and orders the results from the highest salary to the lowest salary using the ORDER BY clause.
SELECT
EmpID,
EmpName,
DeptID,
Salary
FROM
EMPLOYEE
ORDER BY
Salary DESC;

     
 
what is notes.io
 

Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 14 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.