{"id":17562,"date":"2024-08-07T12:58:33","date_gmt":"2024-08-07T07:28:33","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=17562"},"modified":"2024-08-07T17:53:40","modified_gmt":"2024-08-07T12:23:40","slug":"oracle-sql-queries-for-interview","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/oracle-sql-queries-for-interview\/","title":{"rendered":"Oracle SQL Queries for Interview"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p>In today&#8217;s data-driven world, proficiency in SQL (Structured Query Language) is essential for any aspiring data professional. Oracle SQL, in particular, is widely used in enterprise environments for managing and manipulating relational databases. If you&#8217;re preparing for an interview that involves Oracle SQL, it&#8217;s crucial to familiarize yourself with common queries and concepts that are often tested. This blog will cover various Oracle <a href=\"https:\/\/www.h2kinfosys.com\/blog\/top-sql-interview-questions-answers-for-testers\/\" data-type=\"post\" data-id=\"17524\">SQL <\/a>queries and concepts that you should master to excel in your Oracle SQL Queries for Interview.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Basic SQL Queries<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Selecting Data<\/h3>\n\n\n\n<p>The most fundamental operation in SQL is the <code>SELECT<\/code> statement, used to retrieve data from a database.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>SELECT * FROM employees;<br><\/code><\/pre>\n\n\n\n<p>This query retrieves all columns from the <code>employees<\/code> table. However, it&#8217;s often more efficient to specify only the columns you need:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>SELECT employee_id, first_name, last_name FROM employees;<br><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Filtering Data<\/h3>\n\n\n\n<p>The <code>WHERE<\/code> clause is used to filter records based on specified conditions.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>SELECT first_name, last_name FROM employees WHERE department_id = 10;<br><\/code><\/pre>\n\n\n\n<p>This query retrieves the first and last names of employees who work in department 10.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Sorting Data<\/h3>\n\n\n\n<p>To sort the result set, you use the <code>ORDER BY<\/code> clause.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>SELECT first_name, last_name FROM employees ORDER BY last_name ASC;<br><\/code><\/pre>\n\n\n\n<p>This query retrieves the first and last names of employees sorted by their last name in ascending order.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Aggregating Data<\/h3>\n\n\n\n<p>Aggregation functions, such as <code>COUNT<\/code>, <code>SUM<\/code>, <code>AVG<\/code>, <code>MAX<\/code>, and <code>MIN<\/code>, are used to perform calculations on a set of values.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>SELECT COUNT(*) FROM employees WHERE department_id = 10;<br><\/code><\/pre>\n\n\n\n<p>This query counts the number of employees in department 10.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Intermediate SQL <a href=\"https:\/\/www.iitworkforce.com\/software-development\/\" rel=\"nofollow noopener\" target=\"_blank\">Queries<\/a><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Joining Tables<\/h3>\n\n\n\n<p>Joins are used to combine rows from two or more tables based on a related column between them.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Inner Join<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">sqlCopy code<code>SELECT e.first_name, e.last_name, d.department_name\nFROM employees e\nINNER JOIN departments d ON e.department_id = d.department_id;\n<\/code><\/pre>\n\n\n\n<p>This query retrieves the first name, last name, and department name of employees by joining the <code>employees<\/code> and <code>departments<\/code> tables.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Left Join<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>SELECT e.first_name, e.last_name, d.department_name<br>FROM employees e<br>LEFT JOIN departments d ON e.department_id = d.department_id;<br><\/code><\/pre>\n\n\n\n<p>This query retrieves all employees&#8217; names and their respective departments, including employees who are not assigned to any department.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Grouping Data<\/h3>\n\n\n\n<p>The <code>GROUP BY<\/code> clause groups rows that have the same values in specified columns into summary rows.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>SELECT department_id, COUNT(*) AS employee_count<br>FROM employees<br>GROUP BY department_id;<br><\/code><\/pre>\n\n\n\n<p>This query retrieves the number of employees in each department.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Subqueries<\/h3>\n\n\n\n<p>Subqueries are nested queries used within another SQL query to perform additional operations.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>SELECT first_name, last_name<br>FROM employees<br>WHERE salary > (SELECT AVG(salary) FROM employees);<br><\/code><\/pre>\n\n\n\n<p>This query retrieves the first and last names of employees whose salary is above the average salary.<\/p>\n\n\n\n<p><strong>Recommended To Raed Also: <\/strong><a href=\"https:\/\/www.h2kinfosys.com\/courses\/qa-online-training-course-details\/\">Manual Testing Online Course with Certificate<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Advanced SQL Queries<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Using Window Functions<\/h3>\n\n\n\n<p>Window functions perform calculations across a set of table rows related to the current row.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>SELECT first_name, last_name, salary,<br>RANK() OVER (ORDER BY salary DESC) AS salary_rank<br>FROM employees;<br><\/code><\/pre>\n\n\n\n<p>This query assigns a rank to each employee based on their salary, in descending order.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Common Table Expressions (CTEs)<\/h3>\n\n\n\n<p>CTEs are temporary result sets defined within the execution scope of a <code>SELECT<\/code>, <code>INSERT<\/code>, <code>UPDATE<\/code>, or <code>DELETE<\/code> statement.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>WITH department_employees AS (<br>    SELECT department_id, COUNT(*) AS employee_count<br>    FROM employees<br>    GROUP BY department_id<br>)<br>SELECT d.department_name, de.employee_count<br>FROM departments d<br>JOIN department_employees de ON d.department_id = de.department_id;<br><\/code><\/pre>\n\n\n\n<p>This query uses a CTE to count the number of employees in each department and then joins it with the <code>departments<\/code> table to retrieve department names.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Handling NULL Values<\/h3>\n\n\n\n<p>Oracle SQL provides functions to handle NULL values, such as <code>NVL<\/code>, <code>COALESCE<\/code>, and <code>NULLIF<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>SELECT first_name, last_name, NVL(commission_pct, 0) AS commission<br>FROM employees;<br><\/code><\/pre>\n\n\n\n<p>This query retrieves the first name, last name, and commission percentage of employees, replacing NULL values with 0.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Hierarchical Queries<\/h3>\n\n\n\n<p>Hierarchical queries are used to retrieve data based on a hierarchical relationship within the same table.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>SELECT employee_id, first_name, last_name, manager_id<br>FROM employees<br>START WITH manager_id IS NULL<br>CONNECT BY PRIOR employee_id = manager_id;<br><\/code><\/pre>\n\n\n\n<p>This query retrieves the hierarchy of employees starting with the top-level managers.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Pivoting Data<\/h3>\n\n\n\n<p>Pivoting involves rotating rows into columns to present summary data.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>SELECT *<br>FROM (SELECT department_id, job_id, salary FROM employees)<br>PIVOT (SUM(salary) FOR job_id IN ('IT_PROG', 'ST_CLERK', 'SA_MAN'));<br><\/code><\/pre>\n\n\n\n<p>This query pivots the salaries of employees by their job roles within each department.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Performance Tuning Tips<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Indexes<\/h3>\n\n\n\n<p>Indexes improve the speed of data retrieval operations. However, they can slow down <code>INSERT<\/code>, <code>UPDATE<\/code>, and <code>DELETE<\/code> operations.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>TE INDEX idx_employee_last_name ON employees(last_name);<br><\/code><\/pre>\n\n\n\n<p>This query creates an index on the <code>last_name<\/code> column of the <code>employees<\/code> table.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Execution Plans<\/h3>\n\n\n\n<p>Analyzing execution plans helps identify performance bottlenecks in SQL queries.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>EXPLAIN PLAN FOR<br>SELECT * FROM employees WHERE last_name = 'Smith';<br><\/code><\/pre>\n\n\n\n<p>This query provides the execution plan for retrieving employees with the last name &#8216;Smith&#8217;.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Using Hints<\/h3>\n\n\n\n<p>Oracle SQL allows you to use hints to influence the optimizer&#8217;s choice of execution plan.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>SELECT \/*+ INDEX(employees idx_employee_last_name) *\/<br>first_name, last_name FROM employees WHERE last_name = 'Smith';<br><\/code><\/pre>\n\n\n\n<p>This query uses a hint to suggest using the <code>idx_employee_last_name<\/code> index.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Avoiding Full Table Scans<\/h3>\n\n\n\n<p>Full table scans can be costly in terms of performance. Ensure your queries use indexes where appropriate.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>SELECT first_name, last_name FROM employees WHERE employee_id = 100;<br><\/code><\/pre>\n\n\n\n<p>This query uses the primary key index on <code>employee_id<\/code> for efficient retrieval.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Optimizing Joins<\/h3>\n\n\n\n<p>Ensure that your join conditions are indexed and that you&#8217;re joining on the most restrictive condition first.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>SELECT e.first_name, e.last_name, d.department_name<br>FROM employees e<br>JOIN departments d ON e.department_id = d.department_id<br>WHERE e.salary > 50000;<br><\/code><\/pre>\n\n\n\n<p>This query joins the <code>employees<\/code> and <code>departments<\/code> tables, filtering employees with a salary greater than 50,000.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common Interview Questions and Solutions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. Retrieve the Top N Salaries<\/h3>\n\n\n\n<p>Interviewers often ask you to retrieve the top N salaries in a table.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>SELECT * FROM (<br>    SELECT first_name, last_name, salary,<br>    RANK() OVER (ORDER BY salary DESC) AS salary_rank<br>    FROM employees<br>) WHERE salary_rank &lt;= 5;<br><\/code><\/pre>\n\n\n\n<p>This query retrieves the top 5 highest salaries.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Find Employees with the Highest Salary in Each Department<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>SELECT department_id, first_name, last_name, salary<br>FROM (<br>    SELECT department_id, first_name, last_name, salary,<br>    RANK() OVER (PARTITION BY department_id ORDER BY salary DESC) AS salary_rank<br>    FROM employees<br>) WHERE salary_rank = 1;<br><\/code><\/pre>\n\n\n\n<p>This query retrieves the employees with the highest salary in each department.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Identify Duplicate Records<\/h3>\n\n\n\n<p>Interviewers might ask you to identify duplicate records in a table.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>SELECT first_name, last_name, COUNT(*)<br>FROM employees<br>GROUP BY first_name, last_name<br>HAVING COUNT(*) > 1;<br><\/code><\/pre>\n\n\n\n<p>This query retrieves duplicate employee names.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Calculate the Difference Between Two Dates<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>SELECT first_name, last_name, hire_date,<br>SYSDATE - hire_date AS days_worked<br>FROM employees;<br><\/code><\/pre>\n\n\n\n<p>This query calculates the number of days an employee has worked.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Update Records Conditionally<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>UPDATE employees<br>SET salary = salary * 1.10<br>WHERE department_id = 10;<br><\/code><\/pre>\n\n\n\n<p>This query increases the salary of employees in department 10 by 10%.<\/p>\n\n\n\n<p><strong>Recommended To Raed Also<\/strong>: <a href=\"https:\/\/www.h2kinfosys.com\/courses\/qa-online-training-course-details\/\">Manual Testing free Course with Certificate<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Mastering Oracle SQL queries is crucial for acing any interview related to database management and data analysis. This guide has covered a range of queries from basic to advanced, as well as performance tuning tips and common interview questions. By practicing these queries and understanding the underlying concepts, you&#8217;ll be well-prepared to tackle any Oracle SQL interview questions that come your way. Happy learning, and good luck with your interview!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction In today&#8217;s data-driven world, proficiency in SQL (Structured Query Language) is essential for any aspiring data professional. Oracle SQL, in particular, is widely used in enterprise environments for managing and manipulating relational databases. If you&#8217;re preparing for an interview that involves Oracle SQL, it&#8217;s crucial to familiarize yourself with common queries and concepts that [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":17477,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-17562","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/17562","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=17562"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/17562\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/17477"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=17562"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=17562"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=17562"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}