0% found this document useful (0 votes)
10 views9 pages

DBMS 3b(employee department location )

The document provides a practical guide for SQL queries based on a schema involving Employee, Departments, and Locations tables. It includes sample SQL queries to find employee salaries, details, and department statistics, along with instructions for setting up tables and inserting sample data. Additionally, it lists potential oral exam questions and answers related to SQL concepts, ensuring comprehensive exam preparation.

Uploaded by

Devil Creations
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)
10 views9 pages

DBMS 3b(employee department location )

The document provides a practical guide for SQL queries based on a schema involving Employee, Departments, and Locations tables. It includes sample SQL queries to find employee salaries, details, and department statistics, along with instructions for setting up tables and inserting sample data. Additionally, it lists potential oral exam questions and answers related to SQL concepts, ensuring comprehensive exam preparation.

Uploaded by

Devil Creations
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/ 9

ChatGPT - SQL Queries Practice

Consider Following Schema


Employee (Employee_id, First_name, last_name , hire_date,
salary,
Job_title, manager_id, department_id)
Departments(Department_id, Department_name,
Manager_id,
Location_id)
Locations(location_id ,street_address ,postal_code, city,
state, country_id)
1. Write a query to find the names (first_name, last_name),
the salary of
the employees who earn the same salary as the minimum
salary for all
departments.
2. Write a query to display the employee ID, first name, last
names, salary
of all employees whose salary is above average for their
departments.
3. Write a query to find the employee id, name (last_name)
along with
their manager_id, manager name (last_name).
4. Find the names and hire date of the employees who were
hired after
'Jones'.
5. Write a query to get the department name and number
of employees in the
department.

tell me how to perform this practical in exam along with


sample data for insertion and testing

also tell the oral questions examiner can ask for this

To prepare for this practical, let's break down each question, providing sample SQL queries
and inserting test data to illustrate how to test each query. I'll also cover possible oral
questions the examiner might ask and concepts you should understand.

Printed with ChatGPT to PDF 1/9


Step 1: Create Tables and Insert Sample Data
Create each table ( Employee , Departments , and Locations ) and add some test data. This
will allow you to run and test your queries.

Table Creation

sql

-- Create Employee table


CREATE TABLE Employee (
Employee_id INT PRIMARY KEY,
First_name VARCHAR(50),
Last_name VARCHAR(50),
Hire_date DATE,
Salary DECIMAL(10, 2),
Job_title VARCHAR(50),
Manager_id INT,
Department_id INT
);

-- Create Departments table


CREATE TABLE Departments (
Department_id INT PRIMARY KEY,
Department_name VARCHAR(50),
Manager_id INT,
Location_id INT
);

-- Create Locations table


CREATE TABLE Locations (
Location_id INT PRIMARY KEY,
Street_address VARCHAR(100),
Postal_code VARCHAR(20),
City VARCHAR(50),
State VARCHAR(50),
Country_id VARCHAR(10)
);

Sample Data for Testing

sql

Printed with ChatGPT to PDF 2/9


-- Insert sample data into Employee
INSERT INTO Employee (Employee_id, First_name, Last_name, Hire_date, Salary,
Job_title, Manager_id, Department_id)
VALUES
(1, 'John', 'Doe', '2021-01-01', 5000, 'Developer', NULL, 1),
(2, 'Jane', 'Smith', '2022-05-15', 7000, 'Developer', 1, 1),
(3, 'Alice', 'Jones', '2020-03-10', 6000, 'Analyst', 1, 2),
(4, 'Mark', 'Taylor', '2023-06-20', 7000, 'Analyst', 3, 2),
(5, 'Emily', 'Davis', '2021-11-25', 8000, 'Manager', NULL, 1);

-- Insert sample data into Departments


INSERT INTO Departments (Department_id, Department_name, Manager_id, Location_id)
VALUES
(1, 'Engineering', 5, 101),
(2, 'Finance', 3, 102);

-- Insert sample data into Locations


INSERT INTO Locations (Location_id, Street_address, Postal_code, City, State,
Country_id)
VALUES
(101, '123 Tech Park', '10001', 'New York', 'NY', 'US'),
(102, '456 Finance St', '20002', 'Washington', 'DC', 'US');

Step 2: Writing and Testing Queries


Now let's write the queries based on the questions.

1. Find the names and salary of employees who earn the same as the minimum salary
for all departments.

sql

SELECT First_name, Last_name, Salary


FROM Employee
WHERE Salary = (SELECT MIN(Salary) FROM Employee);

2. Display employee details whose salary is above the department average.

sql

Printed with ChatGPT to PDF 3/9


SELECT Employee_id, First_name, Last_name, Salary
FROM Employee e
WHERE Salary > (
SELECT AVG(Salary)
FROM Employee
WHERE Department_id = e.Department_id
);

3. Find employee id, last name, manager id, and manager's last name.

sql

SELECT e.Employee_id, e.Last_name AS Employee_Name, e.Manager_id, m.Last_name AS


Manager_Name
FROM Employee e
LEFT JOIN Employee m ON e.Manager_id = m.Employee_id;

4. Find employees hired after 'Jones'.

sql

SELECT First_name, Last_name, Hire_date


FROM Employee
WHERE Hire_date > (
SELECT Hire_date FROM Employee WHERE Last_name = 'Jones'
);

5. Get department name and number of employees in each department.

sql

SELECT d.Department_name, COUNT(e.Employee_id) AS Number_of_Employees


FROM Departments d
LEFT JOIN Employee e ON d.Department_id = e.Department_id
GROUP BY d.Department_name;

Step 3: Practical Demonstration Tips


1. Set up data: Begin by showing the sample data in each table to the examiner.

Printed with ChatGPT to PDF 4/9


2. Run queries: Execute each query and explain the output, discussing any subqueries or
joins involved.

3. Debugging: If a query doesn’t work, calmly check for syntax errors or table name
mismatches.

Sample Oral Questions


1. Explain Subqueries: How does a subquery work? For example, in the first query, how
does it return the minimum salary?

2. Joins: What types of joins are there? Why do we use a LEFT JOIN in question 3?

3. Aggregation: Explain aggregate functions like COUNT , MIN , and AVG .

4. GROUP BY: Why is GROUP BY used in question 5? Can you use GROUP BY without
aggregate functions?

5. Difference between WHERE and HAVING: Where would you use HAVING instead of
WHERE ?

Tips for Exam Preparation


Practice with different data: Test each query on different data to see if it always gives
correct results.

Understand query logic: For each query, know why each component (join, subquery,
condition) is necessary.

Optimize: Learn how to rewrite complex queries for efficiency.

Good luck with your exam!

give questions for oral exam along with answers

Here’s a list of oral exam questions based on the schema and queries provided, along with
model answers to help you prepare.

1. What is a primary key? Why is it important?


Answer: A primary key is a column (or a set of columns) that uniquely identifies each row
in a table. It is important because it ensures that each record is unique and helps

Printed with ChatGPT to PDF 5/9


maintain data integrity. It also allows for efficient indexing and retrieval of data.

2. Explain what a foreign key is and its purpose in a database.


Answer: A foreign key is a column or a set of columns in one table that refers to the
primary key in another table. It is used to link tables together, establishing a relationship
between them. For instance, in our schema, the Department_id in the Employee table is
a foreign key that references the Department_id in the Departments table. This
relationship enforces referential integrity, ensuring that values in the foreign key column
must exist in the referenced table.

3. What is a subquery, and how is it used in SQL?


Answer: A subquery is a query nested within another query. It is used to perform
operations on a set of data and return a value or a set of values that can be used by the
main (outer) query. Subqueries are helpful for filtering data, performing calculations,
and retrieving information that depends on other data in the database. For example, in
the query to find employees with the same salary as the minimum salary, we used a
subquery to get the minimum salary for comparison.

4. Explain the difference between WHERE and HAVING clauses.


Answer: The WHERE clause is used to filter records before any grouping or aggregation
occurs, while the HAVING clause is used to filter records after the GROUP BY operation
and is applied to aggregated data. WHERE is for individual row filtering, and HAVING is
for filtering groups created by GROUP BY .

5. Describe what an aggregate function is and give some examples.


Answer: Aggregate functions perform calculations on a set of values and return a single
value. They are commonly used with GROUP BY for summarizing data. Examples include:

COUNT() : Counts the number of rows.

SUM() : Adds up all values in a column.

AVG() : Calculates the average value.

MIN() and MAX() : Find the smallest and largest values, respectively.

6. What is the purpose of the JOIN operation in SQL, and what types
of joins are there?

Printed with ChatGPT to PDF 6/9


Answer: The JOIN operation is used to combine rows from two or more tables based on
a related column between them. There are several types of joins:

INNER JOIN: Returns only the rows where there is a match in both tables.

LEFT JOIN (or LEFT OUTER JOIN): Returns all rows from the left table and matched
rows from the right table. If there is no match, NULL is returned for columns from
the right table.

RIGHT JOIN (or RIGHT OUTER JOIN): Returns all rows from the right table and
matched rows from the left table.

FULL JOIN (or FULL OUTER JOIN): Returns all rows where there is a match in one of
the tables. Rows without matches get NULLs in the columns of the other table.

7. In Query 2, how does SQL calculate the department average salary?


Answer: In Query 2, the SQL uses a subquery that calculates the average salary for
employees within the same department. For each employee in the main query, SQL
compares the employee’s salary to the average salary returned by the subquery. The
subquery groups employees by Department_id and calculates the average using the
AVG() function.

8. Why did we use LEFT JOIN in Query 3 instead of an INNER JOIN ?


Answer: We used a LEFT JOIN to ensure that all employees are included in the result,
even if they don’t have a manager. An INNER JOIN would exclude employees who don’t
have a manager because there would be no matching row in the join. The LEFT JOIN
allows for those rows to appear in the output with NULLs in the manager columns.

9. Explain what GROUP BY does in SQL.


Answer: The GROUP BY statement is used to arrange identical data into groups. It is
commonly used with aggregate functions, such as COUNT , SUM , and AVG , to apply these
functions to each group of data. For example, in Query 5, we group by Department_name
to get the count of employees in each department.

10. What are indexes in a database, and why are they important?
Answer: Indexes are database objects that improve the speed of data retrieval
operations. They function similarly to an index in a book, where it allows the database to
find rows faster by indexing columns. Indexes are important for enhancing performance,

Printed with ChatGPT to PDF 7/9


especially for large datasets. However, they also require storage and can slow down
insert, update, and delete operations because the index must be updated.

11. How would you handle duplicate records in a table?


Answer: To handle duplicates, I can use the DISTINCT keyword in a SELECT statement
to return unique records. If I want to delete duplicates, I can use ROW_NUMBER() along
with PARTITION BY and ORDER BY to identify duplicates, then delete rows where the
row number is greater than 1.

12. Why might we want to use a VIEW in SQL?


Answer: A VIEW is a virtual table created by a query that can be used as if it were a
regular table. Views are useful for simplifying complex queries, providing a level of
abstraction, enforcing security by limiting access to specific data, and improving
maintainability by allowing us to update the view’s definition rather than individual
queries.

13. Can you explain the purpose of NULL in SQL?


Answer: NULL represents the absence of a value or unknown data in SQL. It is different
from zero or an empty string. NULL is used when a field does not contain any data. SQL
provides functions like IS NULL , IS NOT NULL , COALESCE , and IFNULL to handle and
work with NULL values.

14. How can you compare dates in SQL?


Answer: In SQL, dates can be compared using operators like > , < , = , <= , and >= . For
example, we can compare hire dates using a condition like WHERE Hire_date > '2022-
01-01' . SQL also has functions like DATEDIFF() to calculate the difference between
dates and DATEPART() to extract parts of a date.

15. In Query 4, how does SQL know to return employees hired after
'Jones'?
Answer: In Query 4, SQL first identifies the hire date of the employee with the last name
'Jones' using a subquery. Then, it compares this hire date with each employee's hire date
in the WHERE clause of the main query. Only employees hired after this date are
returned.

Printed with ChatGPT to PDF 8/9


These questions and answers cover foundational SQL concepts and more advanced topics
related to the schema and queries. Practice explaining these concepts in your own words, as
understanding the logic behind each query will help you answer confidently during the
exam.

Printed with ChatGPT to PDF 9/9

You might also like