Assignment - 06
Assignment - 06
edu
002832284 DMDD
Assignment - 6
Answer the following questions on the provided schema for the table and sample record INSERT
statements:
Table definition:
first_name VARCHAR(50),
last_name VARCHAR(50),
);
Sample records:
VALUES
(1, 'John', 'Doe', 50000, 'HR'),
1. Write a query to insert data into the `employees` table from another table `contractors`
with the same structure, excluding those in the 'HR' department.
1
Om Ashok Dhinoja [email protected]
002832284 DMDD
2. How can you delete employees from the `employees` table who have a salary less or
equal to $60,000 ?
3. How can you update the salary of employees in the 'IT' department by increasing it by
10%?
4. Write a query to select the top 3 highest salaries from the `employees` table.
5. How can you select employees whose first name starts with 'J' and ends with 'n'?
6. Write a query to select employees who are either in the 'HR' department or have a salary
greater than 60000, but not both.
2
Om Ashok Dhinoja [email protected]
002832284 DMDD
7. How can you get the total salary for each department using the `employees` table?
8. Write a query to select employees from the `employees` table, sorted by department in
ascending order, and then by salary in descending order within each department.
9. How can you create a view that shows the average salary for each department from the
`employees` table?
10. What is a strategy to optimize a query that frequently retrieves department and average
salary columns data from the `employees` table?
Ans. To improve the performance of queries that frequently involve the "department"
and average salary data in the "employees" table, several techniques can be applied:
a. Materialized Views: Creating a materialized view that pre-computes the average
salary by department can reduce the need to calculate it repeatedly during each
query. Since the results are stored separately, the query execution time is reduced.
However, regular refreshing of the view is required to ensure it reflects the most up-
to-date information.
b. Indexing: Adding an index on columns such as "department" can significantly
speed up searches, joins, and grouping operations. Indexes enable the database to
locate relevant rows faster by organizing data in a structure that facilitates quicker
lookups.
c. Optimizing Queries: Writing efficient SQL queries is essential for optimizing
database performance. Avoid using "SELECT *" and instead specify only the
columns that are needed. This minimizes the amount of data that needs to be
processed and transmitted, which can speed up the query execution.
3
Om Ashok Dhinoja [email protected]
002832284 DMDD
d. Partitioning: For large tables, partitioning can be an effective strategy. By dividing
the "employees" table based on a column such as "department," the database can
operate on a smaller subset of data, which reduces the time required for scanning
and processing large datasets. Additionally, indexes can be applied to each partition
for better performance.
e. Caching: Frequently used query results or summaries can be stored in memory
(cache) at the application level. When the same data is needed again, it can be
retrieved from the cache rather than querying the database, which reduces database
load and improves response times.
f. Database Maintenance: Regularly maintaining the database by updating statistics,
rebuilding indexes, and resolving fragmentation is important to keep performance
optimal. Proper maintenance ensures that queries run efficiently and that the
database can continue to perform well under increasing workloads.
These methods, when applied effectively, can greatly enhance query speed and overall
database performance without overburdening the system.