{"id":12476,"date":"2024-08-28T10:15:57","date_gmt":"2024-08-28T04:45:57","guid":{"rendered":"https:\/\/www.placementpreparation.io\/blog\/?p=12476"},"modified":"2024-12-26T17:10:40","modified_gmt":"2024-12-26T11:40:40","slug":"dbms-interview-questions-for-freshers","status":"publish","type":"post","link":"https:\/\/www.placementpreparation.io\/blog\/dbms-interview-questions-for-freshers\/","title":{"rendered":"Top DBMS Interview Questions for Freshers"},"content":{"rendered":"<?xml encoding=\"utf-8\" ?><p>Are you preparing for your first DBMS interview and wondering what questions you might face?<\/p><p>Understanding the key DBMS interview questions for freshers can give you more clarity.<\/p><p>With this guide, you&rsquo;ll be well-prepared to tackle these DBMS interview questions and answers for freshers and make a strong impression in your interview.<\/p><p><a href=\"https:\/\/www.guvi.in\/courses\/database-and-cloud-computing\/sql-for-beginners\/?utm_source=placement_preparation&amp;utm_medium=blog_banner&amp;utm_campaign=dbms_interview_questions_for_freshers_horizontal\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" class=\"alignnone wp-image-10346 size-full\" src=\"https:\/\/www.placementpreparation.io\/blog\/wp-content\/uploads\/2024\/05\/sql-beginners-course-desktop-banner-horizontal.webp\" alt=\"sql beginners course desktop banner horizontal\" width=\"2270\" height=\"600\" srcset=\"https:\/\/www.placementpreparation.io\/blog\/wp-content\/uploads\/2024\/05\/sql-beginners-course-desktop-banner-horizontal.webp 2270w, https:\/\/www.placementpreparation.io\/blog\/wp-content\/uploads\/2024\/05\/sql-beginners-course-desktop-banner-horizontal-300x79.webp 300w, https:\/\/www.placementpreparation.io\/blog\/wp-content\/uploads\/2024\/05\/sql-beginners-course-desktop-banner-horizontal-1024x271.webp 1024w, https:\/\/www.placementpreparation.io\/blog\/wp-content\/uploads\/2024\/05\/sql-beginners-course-desktop-banner-horizontal-768x203.webp 768w, https:\/\/www.placementpreparation.io\/blog\/wp-content\/uploads\/2024\/05\/sql-beginners-course-desktop-banner-horizontal-1536x406.webp 1536w, https:\/\/www.placementpreparation.io\/blog\/wp-content\/uploads\/2024\/05\/sql-beginners-course-desktop-banner-horizontal-2048x541.webp 2048w, https:\/\/www.placementpreparation.io\/blog\/wp-content\/uploads\/2024\/05\/sql-beginners-course-desktop-banner-horizontal-150x40.webp 150w\" sizes=\"(max-width: 2270px) 100vw, 2270px\"><\/a><\/p><h2 id=\"practice-dbms-interview-questions\">Practice DBMS Interview Questions and Answers<\/h2><p>Below are the top 50 DBMS interview questions for freshers with answers:<\/p><h3 id=\"normalize-to-3nf\">1. How do you normalize a database table to 3NF?<\/h3><p><strong>Answer:<\/strong><\/p><p>Normalization involves organizing tables to reduce redundancy and dependency. To achieve 3NF, remove transitive dependencies by ensuring that non-key attributes are not dependent on other non-key attributes.<\/p><h3 id=\"design-employee-table-structure\">2. Design a table structure for storing information about employees, including their department, salary, and manager.<\/h3><p><strong>Answer:<\/strong><\/p><p>Create separate tables for <strong>Employees<\/strong>, <strong>Departments<\/strong>, and <strong>Managers<\/strong> with foreign keys linking them to handle relationships effectively and avoid redundancy.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>CREATE TABLE Employees (<br>\nEmployeeID INT PRIMARY KEY,<br>\nName VARCHAR(100),<br>\nDepartmentID INT,<br>\nManagerID INT,<br>\nSalary DECIMAL(10,2),<br>\nFOREIGN KEY (DepartmentID) REFERENCES Departments(DepartmentID),<br>\nFOREIGN KEY (ManagerID) REFERENCES Managers(ManagerID)<br>\n);<\/p>\n<\/div><\/div><h3 id=\"create-e-commerce-er-diagram\">3. How do you create an ER diagram for an e-commerce database?<\/h3><p><strong>Answer:<\/strong><\/p><p>Identify entities such as <strong>Customers, Orders, Products,<\/strong> and relationships like <strong>Customer-Places-Order, Order-Contains-Product<\/strong>, then map attributes and cardinalities.<\/p><h3 id=\"enforce-referential-integrity\">4. Write a SQL query to enforce referential integrity between two tables: Orders and Customers.<\/h3><p><strong>Answer:<\/strong><\/p><p>Ensure referential integrity by defining a foreign key constraint in the <strong>Orders<\/strong> table referencing the <strong>Customers<\/strong> table.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>ALTER TABLE Orders<br>\nADD CONSTRAINT fk_customer<br>\nFOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID);<\/p>\n<\/div><\/div><h3 id=\"design-library-schema\">5. How would you design a database schema for a library management system?<\/h3><p><strong>Answer:<\/strong><\/p><p>Design tables like <strong>Books<\/strong>, <strong>Members<\/strong>, <strong>Loans<\/strong>, and <strong>Authors<\/strong>, defining relationships with foreign keys and ensuring normalization to 3NF for efficient data management.<\/p><h3 id=\"find-second-highest-salary\">6. Write a SQL query to find the second highest salary from an Employees table.<\/h3><p><strong>Answer:<\/strong><\/p><p>Use the <strong>DISTINCT<\/strong> keyword and a subquery to skip the highest salary and select the next highest one.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>SELECT MAX(Salary) FROM Employees<br>\nWHERE Salary &lt; (SELECT MAX(Salary) FROM Employees);<\/p>\n<\/div><\/div><h3 id=\"retrieve-colleagues-of-john\">7. How do you retrieve all employees who work in the same department as &lsquo;John&rsquo;?<\/h3><p><strong>Answer:<\/strong><\/p><p>Join the <strong>Employees<\/strong> table on the <strong>DepartmentID<\/strong> and filter using a subquery to match <strong>John<\/strong>&lsquo;s department.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>SELECT * FROM Employees<br>\nWHERE DepartmentID = (SELECT DepartmentID FROM Employees WHERE Name = &lsquo;John&rsquo;);<\/p>\n<\/div><\/div><h3 id=\"calculate-total-sales\">8. Write a SQL query to display the total sales by each salesperson, including those who have not made any sales.<\/h3><p><strong>Answer:<\/strong><\/p><p>Use a <strong>LEFT JOIN<\/strong> between the <strong>Sales<\/strong> and <strong>Salesperson<\/strong> tables and group by <strong>SalespersonID<\/strong> to include those with no sales.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>SELECT s.SalespersonID, SUM(s.Amount) AS TotalSales<br>\nFROM Salesperson sp<br>\nLEFT JOIN Sales s ON sp.SalespersonID = s.SalespersonID<br>\nGROUP BY s.SalespersonID;<\/p>\n<\/div><\/div><h3 id=\"update-salary-by-10%\">9. How do you update the salary of employees by 10% if their performance rating is &lsquo;A&rsquo;?<\/h3><p><strong>Answer:<\/strong><\/p><p>Use the <strong>UPDATE<\/strong> statement with a <strong>WHERE<\/strong> clause to filter employees based on their performance rating.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>UPDATE Employees<br>\nSET Salary = Salary * 1.1<br>\nWHERE PerformanceRating = &lsquo;A&rsquo;;<\/p>\n<\/div><\/div><h3 id=\"delete-below-average-sales\">10. Write a SQL query to delete all records from a Sales table where the sale amount is less than the average sale amount.<\/h3><p><strong>Answer:<\/strong><\/p><p>Use a subquery to calculate the average sale amount and delete records below this threshold.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>DELETE FROM Sales<br>\nWHERE Amount &lt; (SELECT AVG(Amount) FROM Sales);<\/p>\n<\/div><\/div><h3 id=\"create-sql-index\">11. How do you create an index on a column to improve query performance in SQL?<\/h3><p><strong>Answer:<\/strong><\/p><p>Create an index on frequently queried columns to speed up search operations, especially on large tables.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>CREATE INDEX idx_employee_name ON Employees(Name);<\/p>\n<\/div><\/div><h3 id=\"define-composite-index\">12. What is a composite index, and how do you create one in SQL?<\/h3><p><strong>Answer:<\/strong><\/p><p>A composite index is an index on multiple columns, improving query performance when filtering by those columns together.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>CREATE INDEX idx_employee_dept ON Employees(DepartmentID, Name);<\/p>\n<\/div><\/div><h3 id=\"identify-slow-queries\">13. How do you identify slow queries and optimize them in a database?<\/h3><p><strong>Answer:<\/strong><\/p><p>Use <strong>EXPLAIN<\/strong> to analyze the query execution plan, then optimize by adding indexes, rewriting queries, or normalizing tables.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>EXPLAIN SELECT * FROM Employees WHERE DepartmentID = 10;<\/p>\n<\/div><\/div><h3 id=\"use-covering-index\">14. Write a SQL query that utilizes a covering index for a query on Employees fetching Name and DepartmentID.<\/h3><p><strong>Answer:<\/strong><\/p><p>Create a covering index that includes all the columns in the <strong>SELECT<\/strong> list to optimize query performance.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>CREATE INDEX idx_covering ON Employees(Name, DepartmentID);<\/p>\n<\/div><\/div><h3 id=\"avoid-performance-degrading-indexes\">15. How do you avoid using indexes in situations where they can degrade performance?<\/h3><p><strong>Answer:<\/strong><\/p><p>Avoid indexing columns with low cardinality, frequent updates, or those involved in wide range queries.<\/p><h3 id=\"ensure-sql-atomicity\">16. How do you ensure that a set of operations in a SQL database is atomic?<\/h3><p><strong>Answer:<\/strong><\/p><p>Use a transaction to group operations and ensure atomicity, either committing all or rolling back in case of an error.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>BEGIN TRANSACTION;<br>\nUPDATE Accounts SET Balance = Balance &ndash; 100 WHERE AccountID = 1;<br>\nUPDATE Accounts SET Balance = Balance + 100 WHERE AccountID = 2;<br>\nCOMMIT;<\/p>\n<\/div><\/div><h3 id=\"rollback-transaction-error\">17. Write a SQL command to roll back a transaction if an error occurs during execution.<\/h3><p><strong>Answer:<\/strong><\/p><p>Use <strong>ROLLBACK<\/strong> within the transaction block to revert changes if an error occurs.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>BEGIN TRANSACTION;<br>\nUPDATE Orders SET Status = &lsquo;Shipped&rsquo; WHERE OrderID = 1;<br>\nIF @@ERROR != 0 ROLLBACK;<br>\nCOMMIT;<\/p>\n<\/div><\/div><h3 id=\"implement-optimistic-concurrency\">18. How do you implement optimistic concurrency control in a database?<\/h3><p><strong>Answer:<\/strong><\/p><p>Use version numbers or timestamps to detect conflicts and allow multiple transactions to execute simultaneously without locking resources.<\/p><h3 id=\"use-sql-savepoint\">19. Write a SQL query that uses a savepoint to partially roll back a transaction.<\/h3><p><strong>Answer:<\/strong><\/p><p>Use <strong>SAVEPOINT<\/strong> to create a point within a transaction to which you can roll back if necessary.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>BEGIN TRANSACTION;<br>\nUPDATE Orders SET Status = &lsquo;Processing&rsquo; WHERE OrderID = 1;<br>\nSAVEPOINT savepoint1;<br>\nUPDATE Orders SET Status = &lsquo;Shipped&rsquo; WHERE OrderID = 2;<br>\nROLLBACK TO savepoint1;<br>\nCOMMIT;<\/p>\n<\/div><\/div><h3 id=\"resolve-sql-deadlocks\">20. How do you resolve deadlocks in a SQL database?<\/h3><p><strong>Answer:<\/strong><\/p><p>Deadlocks can be resolved by ensuring consistent locking order, using short transactions, or configuring deadlock detection mechanisms.<\/p><h3 id=\"grant-user-privileges\">21. How do you grant specific privileges to a user in a SQL database?<\/h3><p><strong>Answer:<\/strong><\/p><p>Use the <strong>GRANT<\/strong> statement to assign specific privileges like <strong>SELECT, INSERT, UPDATE,<\/strong> or <strong>DELETE<\/strong> to a user.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>GRANT SELECT, INSERT ON Employees TO &lsquo;username&rsquo;;<\/p>\n<\/div><\/div><h3 id=\"revoke-all-except-select\">22. Write a SQL query to revoke all privileges from a user except SELECT.<\/h3><p><strong>Answer:<\/strong><\/p><p>Use the <strong>REVOKE<\/strong> statement to remove all but the <strong>SELECT<\/strong> privilege.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>REVOKE ALL PRIVILEGES ON Employees FROM &lsquo;username&rsquo;;<br>\nGRANT SELECT ON Employees TO &lsquo;username&rsquo;;<\/p>\n<\/div><\/div><h3 id=\"implement-row-level-security\">23. How do you implement row-level security in a database?<\/h3><p><strong>Answer:<\/strong><\/p><p>Use views or policies to restrict access to specific rows in a table based on user roles or attributes.<\/p><h3 id=\"enforce-foreign-key-constraint\">24. Write a SQL query to enforce a foreign key constraint between two tables.<\/h3><p><strong>Answer:<\/strong><\/p><p>Use <strong>FOREIGN KEY<\/strong> constraints to enforce referential integrity between related tables.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>ALTER TABLE Orders<br>\nADD CONSTRAINT fk_customer_id<br>\nFOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID);<\/p>\n<\/div><\/div><h3 id=\"handle-unique-constraint-insertion\">25. How do you ensure data integrity when inserting records into a table with a unique constraint?<\/h3><p><strong>Answer:<\/strong><\/p><p>Use <strong>INSERT IGNORE<\/strong> or <strong>ON CONFLICT<\/strong> clauses to handle unique constraint violations gracefully.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>INSERT INTO Employees (EmployeeID, Name) VALUES (1, &lsquo;John&rsquo;)<br>\nON CONFLICT (EmployeeID) DO NOTHING;<\/p>\n<\/div><\/div><h3 id=\"use-cte-in-query\">26. How do you use a common table expression (CTE) in a SQL query?<\/h3><p><strong>Answer:<\/strong><\/p><p>Use CTEs to define a temporary result set that can be referenced within the main query for improved readability and maintainability.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>WITH EmployeeCTE AS (<br>\nSELECT EmployeeID, Name FROM Employees WHERE DepartmentID = 1<br>\n)<br>\nSELECT * FROM EmployeeCTE WHERE Name LIKE &lsquo;A%&rsquo;;<\/p>\n<\/div><\/div><h3 id=\"calculate-cumulative-sales-sum\">27. Write a SQL query to calculate the cumulative sum of sales in a Sales table.<\/h3><p><strong>Answer:<\/strong><\/p><p>Use the <strong>SUM()<\/strong> window function to calculate the cumulative sum of a column.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>SELECT SalespersonID, SaleDate,<br>\nSUM(Amount) OVER (ORDER BY SaleDate) AS CumulativeSales<br>\nFROM Sales;<\/p>\n<\/div><\/div><h3 id=\"find-employee-subordinates\">28. How do you implement a recursive query in SQL to find all subordinates of an employee?<\/h3><p><strong>Answer:<\/strong><\/p><p>Use a recursive CTE to traverse hierarchical data like organizational charts.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>WITH RECURSIVE Subordinates AS (<br>\nSELECT EmployeeID, Name FROM Employees WHERE ManagerID = 1<br>\nUNION<br>\nSELECT e.EmployeeID, e.Name FROM Employees e<br>\nINNER JOIN Subordinates s ON e.ManagerID = s.EmployeeID<br>\n)<br>\nSELECT * FROM Subordinates;<\/p>\n<\/div><\/div><h3 id=\"find-top-salaries\">29. Write a SQL query to find the top 3 highest salaries in each department.<\/h3><p><strong>Answer:<\/strong><\/p><p>Use the <strong>ROW_NUMBER()<\/strong> window function to rank salaries within each department and filter the top 3.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>WITH RankedSalaries AS (<br>\nSELECT DepartmentID, Name, Salary,<br>\nROW_NUMBER() OVER (PARTITION BY DepartmentID ORDER BY Salary DESC) AS Rank<br>\nFROM Employees<br>\n)<br>\nSELECT * FROM RankedSalaries WHERE Rank &lt;= 3;<\/p>\n<\/div><\/div><h3 id=\"optimize-join-queries\">30. How do you optimize a query that uses a large number of joins?<\/h3><p><strong>Answer:<\/strong><\/p><p>Optimize join queries by ensuring indexes on the join columns, limiting the result set, and considering the join order.<\/p><h3 id=\"create-insert-procedure\">31. How do you create a stored procedure that inserts a new employee and returns the employee ID?<\/h3><p><strong>Answer:<\/strong><\/p><p>Use a stored procedure to encapsulate the insert logic and return the generated <strong>EmployeeID<\/strong>.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>CREATE PROCEDURE InsertEmployee (IN Name VARCHAR(100), OUT<br>\nEmployeeID INT)<br>\nBEGIN<br>\nINSERT INTO Employees (Name) VALUES (Name);<br>\nSET EmployeeID = LAST_INSERT_ID();<br>\nEND;<\/p>\n<\/div><\/div><h3 id=\"update-timestamp-trigger\">32. Write a trigger that automatically updates a LastModified timestamp when a record in a table is updated.<\/h3><p><strong>Answer:<\/strong><\/p><p>Use an <strong>AFTER UPDATE<\/strong> trigger to update the <strong>LastModified<\/strong> column after a record is modified.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>CREATE TRIGGER UpdateLastModified<br>\nAFTER UPDATE ON Employees<br>\nFOR EACH ROW<br>\nBEGIN<br>\nSET NEW.LastModified = NOW();<br>\nEND;<\/p>\n<\/div><\/div><h3 id=\"enforce-business-rule-trigger\">33. How do you implement a trigger to enforce a business rule in a database?<\/h3><p><strong>Answer:<\/strong><\/p><p>Use triggers to enforce business rules such as preventing negative account balances during transactions.<\/p><h3 id=\"calculate-salesperson-sales\">34. Write a stored procedure that calculates and returns the total sales for a specific salesperson.<\/h3><p><strong>Answer:<\/strong><\/p><p>Encapsulate the sum calculation in a stored procedure to return the total sales for a given <strong>SalespersonID<\/strong>.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>CREATE PROCEDURE GetTotalSales (IN SalespersonID INT, OUT TotalSales DECIMAL)<br>\nBEGIN<br>\nSELECT SUM(Amount) INTO TotalSales FROM Sales WHERE SalespersonID = SalespersonID;<br>\nEND;<\/p>\n<\/div><\/div><h3 id=\"handle-stored-procedure-errors\">35. How do you handle errors in stored procedures to ensure proper rollback in case of failure?<\/h3><p><strong>Answer:<\/strong><\/p><p>Use <strong>DECLARE<\/strong> to define error handlers and <strong>ROLLBACK<\/strong> to revert transactions in case of an error.<\/p><h3 id=\"backup-sql-database\">36. How do you back up a SQL database using a command?<\/h3><p><strong>Answer:<\/strong><\/p><p>Use the <strong>BACKUP<\/strong> command to create a backup of the database, specifying the destination file.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>BACKUP DATABASE myDatabase TO DISK = &lsquo;C:\\Backup\\myDatabase.bak&rsquo;;<\/p>\n<\/div><\/div><h3 id=\"restore-sql-database\">37. Write a SQL command to restore a database from a backup file.<\/h3><p><strong>Answer:<\/strong><\/p><p>Use the <strong>RESTORE<\/strong> command to restore the database from a specified backup file.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>RESTORE DATABASE myDatabase FROM DISK = &lsquo;C:\\Backup\\myDatabase.bak&rsquo;;<\/p>\n<\/div><\/div><h3 id=\"point-in-time-recovery\">38. How do you perform a point-in-time recovery in an SQL database?<\/h3><p><strong>Answer:<\/strong><\/p><p>Perform point-in-time recovery by restoring the full backup and applying transaction logs up to a specific time.<\/p><h3 id=\"take-differential-backup\">39. Write an SQL command to take a differential backup of a database.<\/h3><p><strong>Answer:<\/strong><\/p><p>Use the <strong>BACKUP<\/strong> command with the <strong>DIFFERENTIAL<\/strong> option to back up changes since the last full backup.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>BACKUP DATABASE myDatabase TO DISK = &lsquo;C:\\Backup\\myDatabase_diff.bak&rsquo; WITH DIFFERENTIAL;<\/p>\n<\/div><\/div><h3 id=\"ensure-backup-reliability\">40. How do you ensure that your database backup strategy is reliable?<\/h3><p><strong>Answer:<\/strong><\/p><p>Regularly test backup and restore processes, maintain off-site backups, and use redundant storage solutions.<\/p><h3 id=\"implement-many-to-many-relationship\">41. How do you implement a many-to-many relationship in a relational database?<\/h3><p><strong>Answer:<\/strong><\/p><p>Use a junction table that contains foreign keys referencing the primary keys of the related tables.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>CREATE TABLE CourseStudent (<br>\nCourseID INT,<br>\nStudentID INT,<br>\nFOREIGN KEY (CourseID) REFERENCES Courses(CourseID),<br>\nFOREIGN KEY (StudentID) REFERENCES Students(StudentID)<br>\n);<\/p>\n<\/div><\/div><h3 id=\"implement-star-schema\">42. Write a SQL query to implement a star schema for a sales data warehouse.<\/h3><p><strong>Answer:<\/strong><\/p><p>Design a central fact table for sales and surrounding dimension tables for time, products, and customers.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>CREATE TABLE SalesFact (<br>\nSaleID INT PRIMARY KEY,<br>\nProductID INT,<br>\nCustomerID INT,<br>\nTimeID INT,<br>\nAmount DECIMAL(10,2),<br>\nFOREIGN KEY (ProductID) REFERENCES Products(ProductID),<br>\nFOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID),<br>\nFOREIGN KEY (TimeID) REFERENCES Time(TimeID)<br>\n);<\/p>\n<\/div><\/div><h3 id=\"design-high-availability-database\">43. How do you design a database for a high-availability system?<\/h3><p><strong>Answer:<\/strong><\/p><p>Implement replication, sharding, and clustering, and use failover strategies to ensure high availability.<\/p><h3 id=\"create-audit-log\">44. Write an SQL query to implement an audit log for tracking changes to a specific table.<\/h3><p><strong>Answer:<\/strong><\/p><p>Use a trigger to insert change records into an audit table whenever the original table is modified.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>CREATE TRIGGER LogChanges<br>\nAFTER UPDATE ON Employees<br>\nFOR EACH ROW<br>\nBEGIN<br>\nINSERT INTO EmployeeAudit (EmployeeID, ChangeDate) VALUES (NEW.EmployeeID, NOW());<br>\nEND;<\/p>\n<\/div><\/div><h3 id=\"handle-schema-evolution\">45. How do you handle schema evolution in a database with minimal downtime?<\/h3><p><strong>Answer:<\/strong><\/p><p>Use techniques like online schema changes, versioning, and database migrations to evolve the schema without impacting availability.<\/p><h3 id=\"implement-database-sharding\">46. How do you implement sharding in a distributed database system?<\/h3><p><strong>Answer:<\/strong><\/p><p>Shard the database by partitioning data across multiple servers based on a sharding key like customer ID or region.<\/p><h3 id=\"create-materialized-view\">47. Write an SQL command to create a materialized view that caches query results for faster access.<\/h3><p><strong>Answer:<\/strong><\/p><p>Use the <strong>CREATE MATERIALIZED VIEW<\/strong> statement to create a view that stores query results persistently.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>CREATE MATERIALIZED VIEW SalesSummary AS<br>\nSELECT ProductID, SUM(Amount) AS TotalSales FROM Sales GROUP BY ProductID;<\/p>\n<\/div><\/div><h3 id=\"handle-distributed-consistency\">48. How do you handle data consistency in a distributed database with eventual consistency?<\/h3><p><strong>Answer:<\/strong><\/p><p>Use strategies like conflict resolution, versioning, and quorum-based writes to ensure data consistency in distributed systems.<\/p><h3 id=\"calculate-moving-average\">49. Write an SQL query that utilizes a window function to calculate a moving average.<\/h3><p><strong>Answer:<\/strong><\/p><p>Use the <strong>AVG()<\/strong> window function with a specified window frame to calculate the moving average.<\/p><div class=\"su-note\" style=\"border-color:#e5dbc7;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\"><div class=\"su-note-inner su-u-clearfix su-u-trim\" style=\"background-color:#FFF5E1;border-color:#ffffff;color:#333333;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">\n<p>SELECT Date,<br>\nAVG(Sales) OVER (ORDER BY Date ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) AS MovingAvg<br>\nFROM SalesData;<\/p>\n<\/div><\/div><h3 id=\"ensure-data-privacy\">50. How do you ensure data privacy and compliance in a database?<\/h3><p><strong>Answer:<\/strong><\/p><p>Implement encryption, access controls, data masking, and logging to ensure compliance with privacy regulations like GDPR.<\/p><h2>Final Words<\/h2><p>Getting ready for an interview can feel overwhelming, but going through these DBMS fresher interview questions can help you feel more confident.<\/p><p>With the right preparation, you&rsquo;ll ace your DBMS interview but don&rsquo;t forget to practice the DBMS basic coding and query-related interview questions too!<\/p><hr><h2>Frequently Asked Questions<\/h2><h3>1. What are the most common interview questions for Database Management Systems?<\/h3><p>Common interview questions for Database Management Systems typically focus on SQL queries, normalization, transactions, and indexing.<\/p><h3>2. What are the important DBMS topics freshers should focus on for interviews?<\/h3><p>Important DBMS topics freshers should focus on include ER models, normalization, SQL, ACID properties, and indexing.<\/p><h3>3. How should freshers prepare for DBMS technical interviews?<\/h3><p>Freshers should prepare for DBMS technical interviews by practicing SQL queries, understanding normalization, and studying key concepts like transactions and indexing.<\/p><h3>4. What strategies can freshers use to solve DBMS programming questions during interviews?<\/h3><p>Freshers can use strategies like breaking down problems into smaller parts and writing out SQL queries step by step to solve DBMS programming questions during interviews.<\/p><h3>5. Should freshers prepare for advanced DBMS topics in interviews?<\/h3><p>Yes, freshers should prepare for advanced DBMS topics like stored procedures, triggers, and transaction management if the role requires in-depth database knowledge.<\/p><hr><h2>Explore More DBMS Resources<\/h2><ul class=\"explore-more\">\n<li><a href=\"https:\/\/www.placementpreparation.io\/blog\/best-websites-to-learn-dbms\/\">DBMS Websites<\/a><\/li>\n<li><a href=\"https:\/\/www.placementpreparation.io\/blog\/best-youtube-channels-to-learn-dbms\/\">DBMS YouTube Channels<\/a><\/li>\n<li><a href=\"https:\/\/www.placementpreparation.io\/blog\/dbms-project-ideas-for-beginners\/\">DBMS Project Ideas<\/a><\/li>\n<li><a href=\"https:\/\/www.placementpreparation.io\/mcq\/dbms\/\">DBMS MCQ<\/a><\/li>\n<\/ul><h2>Explore More Interview Questions<\/h2><ul class=\"explore-more\">\n<li><a href=\"https:\/\/www.placementpreparation.io\/blog\/python-interview-questions-for-freshers\/\">Python<\/a><\/li>\n<li><a href=\"https:\/\/www.placementpreparation.io\/blog\/java-interview-questions-for-freshers\/\">Java<\/a><\/li>\n<li><a href=\"https:\/\/www.placementpreparation.io\/blog\/sql-interview-questions-for-freshers\/\">SQL<\/a><\/li>\n<li><a href=\"https:\/\/www.placementpreparation.io\/blog\/react-interview-questions-for-freshers\/\">React<\/a><\/li>\n<li><a href=\"https:\/\/www.placementpreparation.io\/blog\/javascript-interview-questions-for-freshers\/\">JavaScript<\/a><\/li>\n<li><a href=\"https:\/\/www.placementpreparation.io\/blog\/c-programming-interview-questions-for-freshers\/\">C Programming<\/a><\/li>\n<li><a href=\"https:\/\/www.placementpreparation.io\/blog\/html-interview-questions-for-freshers\/\">HTML<\/a><\/li>\n<li><a href=\"https:\/\/www.placementpreparation.io\/blog\/css-interview-questions-for-freshers\/\">CSS<\/a><\/li>\n<li><a href=\"https:\/\/www.placementpreparation.io\/blog\/angular-interview-questions-for-freshers\/\">Angular<\/a><\/li>\n<li><a href=\"https:\/\/www.placementpreparation.io\/blog\/cpp-interview-questions-for-freshers\/\">C++<\/a><\/li>\n<li><a href=\"https:\/\/www.placementpreparation.io\/blog\/spring-boot-interview-questions-for-freshers\/\">Spring Boot<\/a><\/li>\n<li><a href=\"https:\/\/www.placementpreparation.io\/blog\/node-js-interview-questions-for-freshers\/\">Node JS<\/a><\/li>\n<li><a href=\"https:\/\/www.placementpreparation.io\/blog\/excel-interview-questions-for-freshers\/\">Excel<\/a><\/li>\n<li><a href=\"https:\/\/www.placementpreparation.io\/blog\/c-sharp-interview-questions-for-freshers\/\">C#<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Are you preparing for your first DBMS interview and wondering what questions you might face?Understanding the key DBMS interview questions for freshers can give you more clarity.With this guide, you&rsquo;ll be well-prepared to tackle these DBMS interview questions and answers for freshers and make a strong impression in your interview.Practice DBMS Interview Questions and AnswersBelow [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":12487,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[45],"tags":[],"class_list":["post-12476","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programming-interview-questions"],"_links":{"self":[{"href":"https:\/\/www.placementpreparation.io\/blog\/wp-json\/wp\/v2\/posts\/12476","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.placementpreparation.io\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.placementpreparation.io\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.placementpreparation.io\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.placementpreparation.io\/blog\/wp-json\/wp\/v2\/comments?post=12476"}],"version-history":[{"count":5,"href":"https:\/\/www.placementpreparation.io\/blog\/wp-json\/wp\/v2\/posts\/12476\/revisions"}],"predecessor-version":[{"id":12489,"href":"https:\/\/www.placementpreparation.io\/blog\/wp-json\/wp\/v2\/posts\/12476\/revisions\/12489"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.placementpreparation.io\/blog\/wp-json\/wp\/v2\/media\/12487"}],"wp:attachment":[{"href":"https:\/\/www.placementpreparation.io\/blog\/wp-json\/wp\/v2\/media?parent=12476"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.placementpreparation.io\/blog\/wp-json\/wp\/v2\/categories?post=12476"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.placementpreparation.io\/blog\/wp-json\/wp\/v2\/tags?post=12476"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}