{"id":4964,"date":"2024-01-19T07:25:35","date_gmt":"2024-01-19T07:25:35","guid":{"rendered":"https:\/\/www.digitalogy.co\/blog\/?p=4964"},"modified":"2024-07-19T07:39:19","modified_gmt":"2024-07-19T07:39:19","slug":"sql-interview-questions-and-answers","status":"publish","type":"post","link":"https:\/\/www.digitalogy.co\/blog\/sql-interview-questions-and-answers\/","title":{"rendered":"SQL Top Interview Questions And Answers"},"content":{"rendered":"\n<p>In the ever-evolving landscape of technology, SQL (Structured Query Language) remains a cornerstone for managing and manipulating relational databases. As companies depend more and more on data-driven insights, there is a growing need for qualified SQL specialists. You must prepare for SQL interviews, regardless of your experience level as a database administrator or your interest in data.&nbsp;<\/p>\n\n\n\n<p>This article aims to provide a comprehensive guide to SQL interview questions and their answers, helping you navigate the challenges and stand out during the interview process.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why SQL is important for an interview?&nbsp;<\/h2>\n\n\n\n<p>SQL enables users to interact with relational databases, extract insights, and design efficient database structures. SQL proficiency is often listed as a prerequisite in job descriptions for various positions like data administrator, data analyst, software developer, etc. making it a key factor in candidate selection. It demonstrates problem-solving abilities, understanding of data integrity, and scalability considerations. As a standardized language, SQL&#8217;s versatility ensures its relevance across industries, emphasizing its importance in job interviews.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">SQL Interview Questions and Answers&nbsp;<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. Explain SQL and its importance.&nbsp;<\/h3>\n\n\n\n<p>SQL stands for Structured Query Language and is a domain-specific language used for storing data and information in tabular form. It is also used for managing and manipulating relational databases. It is important for data retrieval, updates, and management, providing a standardized way to interact with databases.&nbsp;<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Explain the between SQL and MySQL.&nbsp;<\/h2>\n\n\n\n<p>SQL is a language, while MySQL is a relational database management system (RDBMS) that utilizes SQL. SQL is used for querying, inserting, updating, and managing data, while MySQL is the software implementing and managing databases.\u00a0<\/p>\n\n\n\n<pre class=\"wp-block-code has-base-3-color has-contrast-2-background-color has-text-color has-background has-link-color wp-elements-9e6de16fb3533ae3bd915772ac65aa9a\"><code>SELECT * FROM employees;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">3. Fetch all the columns from the &#8220;employees&#8221; table.&nbsp;<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>SELECT * FROM employees;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. List the unique job titles from the &#8220;jobs&#8221; table.&nbsp;<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>SELECT DISTINCT job_title FROM jobs;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">5. How to Count the number of employees in each department.&nbsp;<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>SELECT department_id, COUNT(*) as employee_count&nbsp;&nbsp;&nbsp;FROM employees&nbsp;&nbsp;&nbsp;GROUP BY department_id;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">6. Retrieve the names of employees and their corresponding managers.&nbsp;<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>SELECT e.employee_name, m.manager_name&nbsp;&nbsp;&nbsp;FROM employees e&nbsp;&nbsp;&nbsp;LEFT JOIN managers m ON e.manager_id = m.manager_id;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">7. Differentiate between INNER JOIN and LEFT JOIN.<\/h2>\n\n\n\n<p>Only the rows in both tables with matching values are returned by INNER JOIN; non-matching rows are not included. A LEFT JOIN returns all rows from the left table and matching rows from the right table, replacing non-matching rows with NULLs.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">8. How do you remove duplicate records from a table?&nbsp;<\/h2>\n\n\n\n<p>We can use the DISTINCT keyword in conjunction with the SELECT statement to eliminate duplicate records. For example:-&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>SELECT DISTINCT column1, column2 FROM table_name;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">9. Describe the primary key.&nbsp;<\/h2>\n\n\n\n<p>A primary key is a special code that every entry in a table has, guaranteeing its uniqueness and serving as a link between tables.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">10. Find the highest salary in the &#8220;salaries&#8221; table.&nbsp;<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>SELECT MAX(salary) FROM salaries;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">11. List employees who have a salary greater than $50,000.&nbsp;<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>SELECT employee_name&nbsp;&nbsp;&nbsp;FROM employees&nbsp;&nbsp;&nbsp;WHERE salary &gt; 50000;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">12. Explain the concept of normalization.&nbsp;<\/h2>\n\n\n\n<p>Normalization is the process of organizing data in a database to reduce dependencies and redundancies. It entails breaking up big tables into more manageable, related tables and establishing connections between them.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">13. How to Calculate the average salary for each department.&nbsp;<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>SELECT department_id, AVG(salary) as avg_salary&nbsp;&nbsp;&nbsp;FROM employees&nbsp;&nbsp;&nbsp;GROUP BY department_id;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">14. Clarify the purpose of the GROUP BY clause.&nbsp;<\/h2>\n\n\n\n<p>To create summary rows, combine data in designated columns that have the same values using the GROUP BY clause. It is commonly used with aggregate functions such as COUNT, SUM, AVG, MAX, and MIN.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">15. Retrieve employees whose names start with the letter &#8216;A&#8217;.&nbsp;<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>SELECT employee_name&nbsp;&nbsp;&nbsp;FROM employees&nbsp;&nbsp;&nbsp;WHERE employee_name LIKE &#8216;A%&#8217;;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">16. How does the WHERE clause differ from the HAVING clause?&nbsp;<\/h2>\n\n\n\n<p>While the HAVING clause filters the results after grouping and aggregation, the WHERE clause filters the rows before grouping.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">17. Count the number of employees in the &#8220;sales&#8221; department.&nbsp;<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>SELECT COUNT(*) as employee_count&nbsp;&nbsp;&nbsp;FROM employees&nbsp;&nbsp;&nbsp;WHERE department_id = &#8216;sales&#8217;;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">18. Explain the concept of a foreign key.&nbsp;<\/h2>\n\n\n\n<p>A foreign key is a column or set of columns in a table that points to the primary key of another table. This connection between tables ensures referential integrity is maintained.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">19. List the top 5 highest-paid employees from the table.&nbsp;<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>SELECT employee_name, salary&nbsp;&nbsp;&nbsp;&nbsp;FROM employees&nbsp;&nbsp;&nbsp;&nbsp;ORDER BY salary DESC&nbsp;&nbsp;&nbsp;&nbsp;LIMIT 5;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">20. Explain the term self-join?&nbsp;<\/h2>\n\n\n\n<p>A table that is linked to itself is called a self-join. It is useful when creating relationships within a single table, such as in hierarchical data structures.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">21. List employees who joined in the last year.&nbsp;<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>SELECT employee_name&nbsp;&nbsp;&nbsp;&nbsp;FROM employees&nbsp;&nbsp;&nbsp;&nbsp;WHERE hire_date &gt;= DATE_SUB(NOW(), INTERVAL 1 YEAR);<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">22. How to prevent SQL injection?&nbsp;<\/h2>\n\n\n\n<p>To prevent SQL injection, use parameterized queries or prepared statements. By using these methods, user input is handled more like data than executable code.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">23. What is the purpose of the UNION and UNION ALL operators?&nbsp;<\/h2>\n\n\n\n<p>Duplicate rows are eliminated when two or more SELECT queries&#8217; result sets are combined using UNION. UNION ALL includes all rows, including duplicates.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">24. Find the second highest salary.&nbsp;<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>SELECT MAX(salary)&nbsp;&nbsp;&nbsp;&nbsp;FROM employees&nbsp;&nbsp;&nbsp;&nbsp;WHERE salary &lt; (SELECT MAX(salary) FROM employees);<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">25. Retrieve the average salary excluding the lowest and highest salaries.&nbsp;<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>SELECT AVG(salary)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FROM employees&nbsp;&nbsp;&nbsp;&nbsp;WHERE salary NOT IN (SELECT MIN(salary) FROM employees UNION SELECT MAX(salary) FROM employees);<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">26. Describe the concept of a view.&nbsp;<\/h2>\n\n\n\n<p>A view is a simulated table created from the outcome of a SELECT query. It provides a way to represent data from one or more tables without storing the data itself.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">27. Calculate the total salary expense for the company.&nbsp;<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>SELECT SUM(salary) as total_salary_expense&nbsp;&nbsp;&nbsp;&nbsp;FROM employees;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">28. What are stored procedures?&nbsp;<\/h2>\n\n\n\n<p>Precompiled SQL statements are kept in the database as stored procedures. They can be executed by invoking the procedure&#8217;s name, providing abstraction and reusability.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">29. Find the employees with the same job title and department.&nbsp;<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>SELECT job_title, department_id, COUNT(*) as employee_count&nbsp;&nbsp;&nbsp;&nbsp;FROM employees&nbsp;&nbsp;&nbsp;&nbsp;GROUP BY job_title, department_id&nbsp;&nbsp;&nbsp;&nbsp;HAVING COUNT(*) &gt; 1;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">30. Differentiate between TRUNCATE and DELETE statements.&nbsp;<\/h2>\n\n\n\n<p>A DDL command called TRUNCATE deletes every record from a table without recording the deletion of any individual rows. A DML statement called DELETE deletes particular rows while logging each one.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">31. Retrieve the oldest employee in the company.&nbsp;<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>SELECT employee_name, MAX(birth_date) as oldest_birth_date&nbsp;&nbsp;&nbsp;&nbsp;FROM employees;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">32. What is the purpose of the ORDER BY clause?&nbsp;<\/h2>\n\n\n\n<p>We can arrange the result set of a query using the ORDER BY clause, sorting it in either ascending or descending order based on one or more columns.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">33. List employees who have not been assigned a project.&nbsp;<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>SELECT employee_name&nbsp;&nbsp;&nbsp;&nbsp;FROM employees&nbsp;&nbsp;&nbsp;&nbsp;WHERE employee_id NOT IN (SELECT DISTINCT employee_id FROM projects);<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">34. Calculate the total number of projects for each department.&nbsp;<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>SELECT department_id, COUNT(DISTINCT project_id) as project_count&nbsp;&nbsp;&nbsp;&nbsp;FROM projects&nbsp;&nbsp;&nbsp;&nbsp;GROUP BY department_id;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">35. Explain the concept of ACID properties in databases.&nbsp;<\/h2>\n\n\n\n<p>ACID properties (Atomicity, Consistency, Isolation, Durability) ensure the reliability of database transactions by guaranteeing atomic and consistent operations, isolation from concurrent transactions, and durability of committed changes.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">36. Find employees with a salary above the department&#8217;s average salary.&nbsp;<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>SELECT employee_name, salary, department_id&nbsp;&nbsp;&nbsp;&nbsp;FROM employees&nbsp;&nbsp;&nbsp;&nbsp;WHERE salary &gt; (SELECT AVG(salary) FROM employees e2 WHERE e2.department_id = employees.department_id);<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">37. Retrieve the second-highest salary from the Employee table.<\/h2>\n\n\n\n<p>To determine the second-highest salary, employ a subquery with the MAX and WHERE clauses:&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>SELECT MAX(salary)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FROM employees&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WHERE salary &lt; (SELECT MAX(salary) FROM employees);<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">38. Explain the trigger.&nbsp;<\/h2>\n\n\n\n<p>A trigger is a collection of commands that, when certain events happen, such as INSERT, UPDATE, DELETE, or DDL statements, cause the instructions to run automatically.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">39. Retrieve the employee with the highest salary in each department.&nbsp;<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>SELECT department_id, employee_name, salary&nbsp;&nbsp;&nbsp;&nbsp;FROM employees a&nbsp;&nbsp;&nbsp;&nbsp;WHERE salary = (SELECT MAX(salary) FROM employees WHERE department_id = a.department_id);<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">40. Differentiate between a clustered and a non-clustered index.<\/h2>\n\n\n\n<p>A clustered index dictates the physical arrangement of data in a table, unlike a non-clustered index, which doesn&#8217;t influence the data&#8217;s physical order. A table is limited to one clustered index, but it can accommodate multiple non-clustered indexes.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">41. List employees who have worked for more than 5 years.&nbsp;<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>SELECT employee_name&nbsp;&nbsp;&nbsp;&nbsp;FROM employees&nbsp;&nbsp;&nbsp;&nbsp;WHERE DATEDIFF(NOW(), hire_date) &gt; 1825; &#8212; Assuming 365 days in a year<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">42. How do we manage NULL values in SQL?<\/h2>\n\n\n\n<p>To manage NULL values in the WHERE clause, use the conditions IS NULL or IS NOT NULL. Moreover, NULL values can be changed to specified alternatives by using the COALESCE or ISNULL functions.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">43. How to List the departments with more than 10 employees.&nbsp;<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>SELECT department_id, COUNT(*) as employee_count&nbsp;&nbsp;&nbsp;&nbsp;FROM employees&nbsp;&nbsp;&nbsp;&nbsp;GROUP BY department_id&nbsp;&nbsp;&nbsp;&nbsp;HAVING COUNT(*) &gt; 10;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">43. Explain the term cross join.&nbsp;<\/h2>\n\n\n\n<p>Each row from the initial table is paired with every row from the second table, resulting in the Cartesian product of the two tables, commonly referred to as a cross join or Cartesian join.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">44. How to Count the number of employees in each job title and department combination.&nbsp;<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>SELECT job_title, department_id, COUNT(*) as employee_count&nbsp;&nbsp;&nbsp;&nbsp;FROM employees&nbsp;&nbsp;&nbsp;&nbsp;GROUP BY job_title, department_id;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">45. Explain the purpose of the ROLLBACK statement.&nbsp;<\/h2>\n\n\n\n<p>The ROLLBACK statement is employed to reverse transactions that have not been committed, restoring the database to its state before the initiation of the transaction.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">45. Retrieve the average salary for employees in each job title.&nbsp;<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>SELECT job_title, AVG(salary) as avg_salary&nbsp;&nbsp;&nbsp;&nbsp;FROM employees&nbsp;&nbsp;&nbsp;&nbsp;GROUP BY job_title;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">46. How to find the nth highest salary in a table.<\/h2>\n\n\n\n<p>To determine the salary ranked nth highest, one can utilize the LIMIT and OFFSET clauses:&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>SELECT salary&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FROM employees&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ORDER BY salary DESC&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LIMIT 1 OFFSET n-1;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">47. What is CTE?&nbsp;<\/h2>\n\n\n\n<p>A CTE stands for Common Table Expression which is a named temporary result set defined within the scope of a SELECT, INSERT, UPDATE, or DELETE statement. It enhances the readability of complex queries.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">48. Find employees who have the same manager.&nbsp;<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>SELECT manager_id, COUNT(employee_id) as team_size&nbsp;&nbsp;&nbsp;&nbsp;FROM employees&nbsp;&nbsp;&nbsp;&nbsp;GROUP BY manager_id&nbsp;&nbsp;&nbsp;&nbsp;HAVING COUNT(employee_id) &gt; 1;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">49. Explain the purpose of the LIKE operator.&nbsp;<\/h2>\n\n\n\n<p>The purpose of using the LIKE operator is to search for a specified pattern in a column, allowing the use of wildcard characters for flexible string matching.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">50. Calculate the total number of employees in the company.&nbsp;<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>SELECT COUNT(*) as total_employees&nbsp;&nbsp;&nbsp;&nbsp;FROM employees;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">51. Calculate the total number of rows in a table.<\/h2>\n\n\n\n<p>The COUNT() function is employed to compute the overall number of rows in a table:&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>SELECT COUNT( )&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FROM table_name;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">52. What is the purpose of the DISTINCT keyword?&nbsp;<\/h2>\n\n\n\n<p>To remove duplicate rows from a result set, use the DISTINCT keyword to get unique values from a particular column or column.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">53. List employees who have a salary higher than their manager.&nbsp;<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>SELECT e.employee_name, e.salary, m.manager_name, m.manager_salary&nbsp;&nbsp;&nbsp;&nbsp;FROM employees e&nbsp;&nbsp;&nbsp;&nbsp;JOIN managers m ON e.manager_id = m.manager_id&nbsp;&nbsp;&nbsp;&nbsp;WHERE e.salary &gt; m.manager_salary;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">54. Retrieve the latest 5 projects started in the company.&nbsp;<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>SELECT project_name, start_date&nbsp;&nbsp;&nbsp;&nbsp;FROM projects&nbsp;&nbsp;&nbsp;&nbsp;ORDER BY start_date DESC&nbsp;&nbsp;&nbsp;&nbsp;LIMIT 5;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">55. Explain the concept of database transactions.&nbsp;<\/h2>\n\n\n\n<p>A database transaction is a sequence of one or more SQL statements executed as a single, indivisible unit. The consistency and integrity of the database are guaranteed by transactions.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">56. Calculate the average age of employees.&nbsp;<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>SELECT AVG(YEAR(NOW()) &#8211; YEAR(birth_date)) as avg_age&nbsp;&nbsp;&nbsp;&nbsp;FROM employees;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">57. Find employees who have worked on all projects.&nbsp;<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>SELECT employee_id, employee_name&nbsp;&nbsp;&nbsp;&nbsp;FROM employees e&nbsp;&nbsp;&nbsp;&nbsp;WHERE NOT EXISTS (&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SELECT DISTINCT project_id&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FROM projects&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WHERE project_id NOT IN (&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SELECT project_id&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FROM employee_projects&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WHERE employee_id = e.employee_id&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)&nbsp;&nbsp;&nbsp;&nbsp;);<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">58. Locate employees without a designated manager.&nbsp;<\/h2>\n\n\n\n<p>To find employees without a manager, you can use a LEFT JOIN and filter for NULL values in the manager column:&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>SELECT employees.employee_id, employees.employee_name&nbsp;&nbsp;&nbsp;&nbsp;FROM employees&nbsp;&nbsp;&nbsp;&nbsp;LEFT JOIN managers ON employees.manager_id<br>&nbsp;= managers.manager_id&nbsp;&nbsp;&nbsp;&nbsp;WHERE managers.manager_id IS NULL;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>By thoroughly understanding the fundamental concepts and practicing with real-world scenarios, you can confidently navigate SQL interviews. With this comprehensive guide, you&#8217;re well-equipped to tackle a wide range of SQL interview questions and make an impactful lasting impression on potential employers.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the ever-evolving landscape of technology, SQL (Structured Query Language) remains a cornerstone for managing and manipulating relational databases. As companies depend more and more on data-driven insights, there is a growing need for qualified SQL specialists. You must prepare for SQL interviews, regardless of your experience level as a database administrator or your interest &#8230; <a title=\"SQL Top Interview Questions And Answers\" class=\"read-more\" href=\"https:\/\/www.digitalogy.co\/blog\/sql-interview-questions-and-answers\/\" aria-label=\"Read more about SQL Top Interview Questions And Answers\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":5229,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,437,8],"tags":[430,431],"class_list":["post-4964","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","category-interview-questions","category-programming","tag-sql","tag-sql-interview-questions"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v23.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>SQL Top Interview Questions And Answers<\/title>\n<meta name=\"description\" content=\"Discover expert tips and insights to navigate SQL interviews successfully, including commonly asked questions with answers.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.digitalogy.co\/blog\/sql-interview-questions-and-answers\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SQL Top Interview Questions And Answers\" \/>\n<meta property=\"og:description\" content=\"Discover expert tips and insights to navigate SQL interviews successfully, including commonly asked questions with answers.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.digitalogy.co\/blog\/sql-interview-questions-and-answers\/\" \/>\n<meta property=\"og:site_name\" content=\"Digitalogy Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/digitalogycorp\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-01-19T07:25:35+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-07-19T07:39:19+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.digitalogy.co\/blog\/wp-content\/uploads\/2023\/12\/sql-interview.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"800\" \/>\n\t<meta property=\"og:image:height\" content=\"454\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"digitalogy\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DigitalogyCorp\" \/>\n<meta name=\"twitter:site\" content=\"@DigitalogyCorp\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"digitalogy\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"SQL Top Interview Questions And Answers","description":"Discover expert tips and insights to navigate SQL interviews successfully, including commonly asked questions with answers.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.digitalogy.co\/blog\/sql-interview-questions-and-answers\/","og_locale":"en_US","og_type":"article","og_title":"SQL Top Interview Questions And Answers","og_description":"Discover expert tips and insights to navigate SQL interviews successfully, including commonly asked questions with answers.","og_url":"https:\/\/www.digitalogy.co\/blog\/sql-interview-questions-and-answers\/","og_site_name":"Digitalogy Blog","article_publisher":"https:\/\/www.facebook.com\/digitalogycorp\/","article_published_time":"2024-01-19T07:25:35+00:00","article_modified_time":"2024-07-19T07:39:19+00:00","og_image":[{"width":800,"height":454,"url":"https:\/\/www.digitalogy.co\/blog\/wp-content\/uploads\/2023\/12\/sql-interview.webp","type":"image\/webp"}],"author":"digitalogy","twitter_card":"summary_large_image","twitter_creator":"@DigitalogyCorp","twitter_site":"@DigitalogyCorp","twitter_misc":{"Written by":"digitalogy","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.digitalogy.co\/blog\/sql-interview-questions-and-answers\/#article","isPartOf":{"@id":"https:\/\/www.digitalogy.co\/blog\/sql-interview-questions-and-answers\/"},"author":{"name":"digitalogy","@id":"https:\/\/www.digitalogy.co\/blog\/#\/schema\/person\/072e2cb6f23d60b12f6910171f1c1705"},"headline":"SQL Top Interview Questions And Answers","datePublished":"2024-01-19T07:25:35+00:00","dateModified":"2024-07-19T07:39:19+00:00","mainEntityOfPage":{"@id":"https:\/\/www.digitalogy.co\/blog\/sql-interview-questions-and-answers\/"},"wordCount":2441,"commentCount":0,"publisher":{"@id":"https:\/\/www.digitalogy.co\/blog\/#organization"},"image":{"@id":"https:\/\/www.digitalogy.co\/blog\/sql-interview-questions-and-answers\/#primaryimage"},"thumbnailUrl":"https:\/\/www.digitalogy.co\/blog\/wp-content\/uploads\/2023\/12\/sql-interview.webp","keywords":["SQL","SQL Interview Questions"],"articleSection":["Blogs","Interview Questions","Programming"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.digitalogy.co\/blog\/sql-interview-questions-and-answers\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.digitalogy.co\/blog\/sql-interview-questions-and-answers\/","url":"https:\/\/www.digitalogy.co\/blog\/sql-interview-questions-and-answers\/","name":"SQL Top Interview Questions And Answers","isPartOf":{"@id":"https:\/\/www.digitalogy.co\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.digitalogy.co\/blog\/sql-interview-questions-and-answers\/#primaryimage"},"image":{"@id":"https:\/\/www.digitalogy.co\/blog\/sql-interview-questions-and-answers\/#primaryimage"},"thumbnailUrl":"https:\/\/www.digitalogy.co\/blog\/wp-content\/uploads\/2023\/12\/sql-interview.webp","datePublished":"2024-01-19T07:25:35+00:00","dateModified":"2024-07-19T07:39:19+00:00","description":"Discover expert tips and insights to navigate SQL interviews successfully, including commonly asked questions with answers.","breadcrumb":{"@id":"https:\/\/www.digitalogy.co\/blog\/sql-interview-questions-and-answers\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.digitalogy.co\/blog\/sql-interview-questions-and-answers\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.digitalogy.co\/blog\/sql-interview-questions-and-answers\/#primaryimage","url":"https:\/\/www.digitalogy.co\/blog\/wp-content\/uploads\/2023\/12\/sql-interview.webp","contentUrl":"https:\/\/www.digitalogy.co\/blog\/wp-content\/uploads\/2023\/12\/sql-interview.webp","width":800,"height":454,"caption":"sql interview questions"},{"@type":"BreadcrumbList","@id":"https:\/\/www.digitalogy.co\/blog\/sql-interview-questions-and-answers\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.digitalogy.co\/blog\/"},{"@type":"ListItem","position":2,"name":"Blogs","item":"https:\/\/www.digitalogy.co\/blog\/category\/blog\/"},{"@type":"ListItem","position":3,"name":"SQL Top Interview Questions And Answers"}]},{"@type":"WebSite","@id":"https:\/\/www.digitalogy.co\/blog\/#website","url":"https:\/\/www.digitalogy.co\/blog\/","name":"Digitalogy Blog","description":"Insights on Business, Technology and Startups","publisher":{"@id":"https:\/\/www.digitalogy.co\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.digitalogy.co\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.digitalogy.co\/blog\/#organization","name":"Digitalogy","url":"https:\/\/www.digitalogy.co\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.digitalogy.co\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.digitalogy.co\/blog\/wp-content\/uploads\/2023\/11\/digitalogy-logo.png","contentUrl":"https:\/\/www.digitalogy.co\/blog\/wp-content\/uploads\/2023\/11\/digitalogy-logo.png","width":480,"height":480,"caption":"Digitalogy"},"image":{"@id":"https:\/\/www.digitalogy.co\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/digitalogycorp\/","https:\/\/x.com\/DigitalogyCorp"]},{"@type":"Person","@id":"https:\/\/www.digitalogy.co\/blog\/#\/schema\/person\/072e2cb6f23d60b12f6910171f1c1705","name":"digitalogy","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.digitalogy.co\/blog\/#\/schema\/person\/image\/","url":"https:\/\/www.digitalogy.co\/blog\/wp-content\/litespeed\/avatar\/8593cb63965f17c97fb1bb70ca59f7e7.jpg?ver=1776822597","contentUrl":"https:\/\/www.digitalogy.co\/blog\/wp-content\/litespeed\/avatar\/8593cb63965f17c97fb1bb70ca59f7e7.jpg?ver=1776822597","caption":"digitalogy"},"sameAs":["https:\/\/www.digitalogy.co\/blog"],"url":"https:\/\/www.digitalogy.co\/blog\/author\/digitalogy\/"}]}},"_links":{"self":[{"href":"https:\/\/www.digitalogy.co\/blog\/wp-json\/wp\/v2\/posts\/4964","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.digitalogy.co\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.digitalogy.co\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.digitalogy.co\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.digitalogy.co\/blog\/wp-json\/wp\/v2\/comments?post=4964"}],"version-history":[{"count":0,"href":"https:\/\/www.digitalogy.co\/blog\/wp-json\/wp\/v2\/posts\/4964\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.digitalogy.co\/blog\/wp-json\/wp\/v2\/media\/5229"}],"wp:attachment":[{"href":"https:\/\/www.digitalogy.co\/blog\/wp-json\/wp\/v2\/media?parent=4964"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.digitalogy.co\/blog\/wp-json\/wp\/v2\/categories?post=4964"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.digitalogy.co\/blog\/wp-json\/wp\/v2\/tags?post=4964"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}