<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Pradeepchandra Reddy S C on Medium]]></title>
        <description><![CDATA[Stories by Pradeepchandra Reddy S C on Medium]]></description>
        <link>https://medium.com/@pradeepchandrareddy95?source=rss-a668ce417d44------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*AKJgkpl4hL5T5MxR</url>
            <title>Stories by Pradeepchandra Reddy S C on Medium</title>
            <link>https://medium.com/@pradeepchandrareddy95?source=rss-a668ce417d44------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 05:01:44 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@pradeepchandrareddy95/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Comprehensive Guide to Subqueries and Nested Queries]]></title>
            <link>https://medium.com/@pradeepchandrareddy95/comprehensive-guide-to-subqueries-and-nested-queries-2d234ff514d7?source=rss-a668ce417d44------2</link>
            <guid isPermaLink="false">https://medium.com/p/2d234ff514d7</guid>
            <category><![CDATA[database]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[mysql]]></category>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[data]]></category>
            <dc:creator><![CDATA[Pradeepchandra Reddy S C]]></dc:creator>
            <pubDate>Wed, 11 Oct 2023 07:03:44 GMT</pubDate>
            <atom:updated>2023-10-11T07:03:44.688Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*O8onU6VMYQOZaI9zolUZig.png" /><figcaption>Subqueries</figcaption></figure><h4>Resources to Brush Up the concepts -</h4><p>✨ “SQL Subqueries” on <a href="https://www.linkedin.com/company/w3schools.com/">W3Schools.com</a> (https:// <a href="https://lnkd.in/gqUe8BgZ">https://lnkd.in/gqUe8BgZ</a>)</p><p>✨ “Subqueries in SQL: A Complete Guide” on <a href="http://salshack.com/">salshack.com</a> (<a href="https://lnkd.in/gVi-cypu">https://lnkd.in/gVi-cypu</a>)</p><h4>Example Questions -</h4><p>1. Retrieve all customers who have made at least one purchase.</p><p>2. Find the names of customers who have not made any purchases.</p><p>3. Get the order details for orders with a total quantity greater than the average quantity of all orders.</p><h4>Some solved questions for you to understand the concepts -</h4><h4>1. Write a query to find the top 5 customers with the highest total order amount.</h4><p><strong>Dataset:</strong> Customers (customer_id, customer_name), Orders (order_id, customer_id, order_date, order_amount)</p><pre>SELECT customer_id, customer_name, total_order_amount<br>FROM Customers<br>INNER JOIN (<br>    SELECT customer_id, SUM(order_amount) as total_order_amount<br>    FROM Orders<br>    GROUP BY customer_id<br>    ORDER BY total_order_amount DESC<br>    LIMIT 5<br>) AS TopCustomers ON Customers.customer_id = TopCustomers.customer_id;</pre><p><strong>Let’s break down the query step by step:</strong></p><ol><li>SELECT customer_id, customer_name, total_order_amount: This part of the query specifies the columns you want to retrieve in the result set. You want to retrieve the customer ID, customer name, and the total order amount for the top 5 customers.</li><li>FROM Customers: This part of the query indicates that you are selecting data from the &quot;Customers&quot; table. The &quot;Customers&quot; table likely contains information about customers, such as their customer ID and customer name.</li><li>INNER JOIN: This clause is used to join the &quot;Customers&quot; table with the subquery.</li><li>(SELECT customer_id, SUM(order_amount) as total_order_amount FROM Orders GROUP BY customer_id ORDER BY total_order_amount DESC LIMIT 5) AS TopCustomers: This is a subquery that calculates the total order amount for each customer by summing the &quot;order_amount&quot; from the &quot;Orders&quot; table. It groups the results by &quot;customer_id,&quot; orders them in descending order of total order amount, and limits the result set to the top 5 customers with the highest total order amounts. The subquery is aliased as &quot;TopCustomers.&quot;</li><li>ON Customers.customer_id = TopCustomers.customer_id: This part of the query specifies the join condition. It tells the database to join the &quot;Customers&quot; table with the subquery &quot;TopCustomers&quot; based on the &quot;customer_id&quot; column. This means that it matches customers in the &quot;Customers&quot; table with the top 5 customers in the subquery based on their IDs.</li></ol><h4>2. Retrieve the names of customers who have placed orders in the past 30 days.</h4><p><strong>Dataset:</strong> Customers (customer_id, customer_name), Orders (order_id, customer_id, order_date)</p><pre>SELECT customer_name<br>FROM Customers<br>WHERE customer_id IN (<br>    SELECT DISTINCT customer_id<br>    FROM Orders<br>    WHERE order_date &gt;= DATE_SUB(CURDATE(), INTERVAL 30 DAY)<br>);</pre><p><strong>Let’s break down the query step by step:</strong></p><ol><li>SELECT customer_name: This part of the query specifies the column you want to retrieve in the result set, which is the &quot;customer_name.&quot;</li><li>FROM Customers: This part of the query indicates that you are selecting data from the &quot;Customers&quot; table. The &quot;Customers&quot; table likely contains information about customers, such as their customer names.</li><li>WHERE customer_id IN (...): This is the filtering condition in the query. It limits the results to only include customer names that meet the specified criteria.</li><li>(SELECT DISTINCT customer_id FROM Orders WHERE order_date &gt;= DATE_SUB(CURDATE(), INTERVAL 30 DAY)): This is a subquery that calculates the list of &quot;customer_id&quot; values of customers who have placed orders in the last 30 days. Let&#39;s break down the subquery further:</li></ol><ul><li>SELECT DISTINCT customer_id: This part of the subquery selects unique customer IDs from the &quot;Orders&quot; table, ensuring that each customer is only included once in the list.</li><li>FROM Orders: This part of the subquery specifies that it is retrieving data from the &quot;Orders&quot; table. The &quot;Orders&quot; table likely contains information about orders, including the &quot;customer_id&quot; and &quot;order_date.&quot;</li><li>WHERE order_date &gt;= DATE_SUB(CURDATE(), INTERVAL 30 DAY): This is the filtering condition within the subquery. It selects orders with an &quot;order_date&quot; that is greater than or equal to the date that is 30 days ago from the current date (i.e., it selects orders from the last 30 days).</li></ul><h4>3. Find the products that have been ordered at least three times.</h4><p><strong>Dataset:</strong> Products (product_id, product_name), Order_ltems (order_id, product_id, quantity)</p><pre>SELECT product_id, product_name<br>FROM Products<br>WHERE product_id IN (<br>    SELECT product_id<br>    FROM Order_Items<br>    GROUP BY product_id<br>    HAVING COUNT(*) &gt;= 3<br>);</pre><p><strong>Let’s break down the query step by step:</strong></p><ol><li>SELECT product_id, product_name: This part of the query specifies the columns you want to retrieve in the result set, which are &quot;product_id&quot; and &quot;product_name.&quot;</li><li>FROM Products: This part of the query indicates that you are selecting data from the &quot;Products&quot; table. The &quot;Products&quot; table likely contains information about various products, including their IDs and names.</li><li>WHERE product_id IN (...): This is the filtering condition in the query. It limits the results to only include products that meet the specified criteria.</li><li>(SELECT product_id FROM Order_Items GROUP BY product_id HAVING COUNT(*) &gt;= 3): This is a subquery that calculates the list of &quot;product_id&quot; values for products that have been ordered at least three times. Let&#39;s break down the subquery further:</li></ol><ul><li>SELECT product_id: This part of the subquery selects the &quot;product_id&quot; from the &quot;Order_Items&quot; table.</li><li>FROM Order_Items: This part of the subquery specifies that it is retrieving data from the &quot;Order_Items&quot; table. The &quot;Order_Items&quot; table likely contains information about the individual items ordered, including the &quot;product_id.&quot;</li><li>GROUP BY product_id: This clause groups the results in the subquery by &quot;product_id,&quot; which means it aggregates the order items by the product they belong to.</li><li>HAVING COUNT(*) &gt;= 3: This is the filtering condition within the subquery. It filters the grouped results to only include product IDs that have a count of three or more. In other words, it selects products that have been ordered at least three times.</li></ul><h4>4. Retrieve the order details for orders placed by customers from a specific city.</h4><p><strong>Dataset:</strong> Customers (customer_id, customer_namb,city), Orders (order_id, customer_id, order_date), Order_Details (order_id, product_id, quantity)</p><pre>SELECT order_id, customer_id, order_date, product_id, quantity<br>FROM Orders<br>INNER JOIN Order_Details ON Orders.order_id = Order_Details.order_id<br>WHERE customer_id IN (<br>    SELECT customer_id<br>    FROM Customers<br>    WHERE city = &#39;YourCityName&#39;<br>);</pre><p><strong>Let’s break down the query step by step:</strong></p><ol><li>SELECT order_id, customer_id, order_date, product_id, quantity: This part of the query specifies the columns you want to retrieve in the result set. You are selecting the order ID, customer ID, order date, product ID, and quantity.</li><li>FROM Orders: This part of the query indicates that you are selecting data from the &quot;Orders&quot; table. The &quot;Orders&quot; table likely contains information about customer orders, including order IDs, customer IDs, and order dates.</li><li>INNER JOIN Order_Details ON Orders.order_id = Order_Details.order_id: This clause performs an inner join between the &quot;Orders&quot; table and the &quot;Order_Details&quot; table based on the &quot;order_id&quot; column. This means it combines order information from both tables for orders that match on the &quot;order_id&quot; field. The &quot;Order_Details&quot; table presumably contains detailed information about the products included in each order, including the product ID and quantity.</li><li>WHERE customer_id IN (...): This is the filtering condition in the query. It limits the results to only include orders placed by customers from a specific city.</li><li>(SELECT customer_id FROM Customers WHERE city = &#39;YourCityName&#39;): This is a subquery that retrieves the customer IDs from the &quot;Customers&quot; table where the city is equal to &#39;YourCityName.&#39; The subquery is used to find all the customer IDs for customers in the specified city.</li></ol><h4>5. Write a query to find the customers who have placed orders for products with a price greater than $100.</h4><p><strong>Dataset:</strong> Customers (customer_id, customer_name), Orders (order_id, customer_id, order_date), Products (product_id, product_name, price), Order_Details (order_id, product_id, quantity)</p><pre>SELECT customer_id, customer_name<br>FROM Customers<br>WHERE customer_id IN (<br>    SELECT DISTINCT O.customer_id<br>    FROM Orders O<br>    JOIN Order_Details OD ON O.order_id = OD.order_id<br>    JOIN Products P ON OD.product_id = P.product_id<br>    WHERE P.price &gt; 100<br>);</pre><p><strong>Let’s break down the query step by step:</strong></p><ol><li>SELECT customer_id, customer_name: This part of the query specifies the columns you want to retrieve in the result set, which are &quot;customer_id&quot; and &quot;customer_name.&quot;</li><li>FROM Customers: This part of the query indicates that you are selecting data from the &quot;Customers&quot; table. The &quot;Customers&quot; table likely contains information about customers, including their IDs and names.</li><li>WHERE customer_id IN (...): This is the filtering condition in the query. It limits the results to only include customers who meet the specified criteria.</li><li>(SELECT DISTINCT O.customer_id FROM Orders O JOIN Order_Details OD ON O.order_id = OD.order_id JOIN Products P ON OD.product_id = P.product_id WHERE P.price &gt; 100): This is a subquery that calculates the list of &quot;customer_id&quot; values for customers who have placed orders for products with a price greater than $100. Let&#39;s break down the subquery further:</li></ol><ul><li>SELECT DISTINCT O.customer_id: This part of the subquery selects unique customer IDs from the &quot;Orders&quot; table. The use of DISTINCT ensures that each customer is only included once in the list.</li><li>FROM Orders O JOIN Order_Details OD ON O.order_id = OD.order_id JOIN Products P ON OD.product_id = P.product_id: This part of the subquery performs multiple joins. It connects the &quot;Orders&quot; table to the &quot;Order_Details&quot; table using the &quot;order_id&quot; and then links the &quot;Order_Details&quot; table to the &quot;Products&quot; table using the &quot;product_id.&quot; This sequence of joins allows you to connect the customer ID in the &quot;Orders&quot; table with the product price in the &quot;Products&quot; table.</li><li>WHERE P.price &gt; 100: This is the filtering condition within the subquery. It selects orders for products with a price greater than $100.</li></ul><h4>6. Get the average order amount for each customer.</h4><p><strong>Dataset:</strong> Customers (customer_id, customer_name), Orders (order_id, customer_id, order_date, order_amount)</p><pre>SELECT C.customer_id, C.customer_name, AVG(O.order_amount) as average_order_amount<br>FROM Customers C<br>INNER JOIN Orders O ON C.customer_id = O.customer_id<br>GROUP BY C.customer_id, C.customer_name;</pre><p><strong>Let’s break down the query step by step:</strong></p><ol><li>SELECT C.customer_id, C.customer_name, AVG(O.order_amount) as average_order_amount: This part of the query specifies the columns you want to retrieve in the result set. It selects the customer ID and name from the &quot;Customers&quot; table and calculates the average order amount for each customer, aliasing it as &quot;average_order_amount.&quot;</li><li>FROM Customers C: This part of the query indicates that you are selecting data from the &quot;Customers&quot; table. The &quot;Customers&quot; table likely contains information about customers, including their customer IDs and names.</li><li>INNER JOIN Orders O ON C.customer_id = O.customer_id: This clause performs an inner join between the &quot;Customers&quot; table (aliased as &quot;C&quot;) and the &quot;Orders&quot; table (aliased as &quot;O&quot;) based on the &quot;customer_id&quot; column. It links customer data with order data based on the customer ID.</li><li>GROUP BY C.customer_id, C.customer_name: This clause groups the results by &quot;customer_id&quot; and &quot;customer_name.&quot; It means that the aggregation function, in this case, AVG, will be applied to the order amounts within each customer group.</li></ol><h4>7. Find the products that have never been ordered.</h4><p><strong>Dataset:</strong> Products (product_id, product_name), Order_Items (order_id, product_id, quantity)</p><pre>SELECT product_id, product_name<br>FROM Products<br>WHERE product_id NOT IN (<br>    SELECT DISTINCT product_id<br>    FROM Order_Items<br>);</pre><p><strong>Let’s break down the query step by step:</strong></p><ol><li>SELECT product_id, product_name: This part of the query specifies the columns you want to retrieve in the result set, which are &quot;product_id&quot; and &quot;product_name.&quot;</li><li>FROM Products: This part of the query indicates that you are selecting data from the &quot;Products&quot; table. The &quot;Products&quot; table likely contains information about various products, including their IDs and names.</li><li>WHERE product_id NOT IN (...): This is the filtering condition in the query. It limits the results to only include products that meet the specified criteria.</li><li>(SELECT DISTINCT product_id FROM Order_Items): This is a subquery that calculates the list of &quot;product_id&quot; values from the &quot;Order_Items&quot; table using the DISTINCT keyword to ensure that each product ID is only included once in the list.</li></ol><h4>8. Retrieve the names of customers who have placed orders on weekends (Saturday or Sunday).</h4><p><strong>Dataset:</strong> Customers (customer_id, customer_name), Orders (order_id, customer_id, order_date)</p><pre>SELECT C.customer_name<br>FROM Customers C<br>WHERE C.customer_id IN (<br>    SELECT O.customer_id<br>    FROM Orders O<br>    WHERE DAYOFWEEK(O.order_date) IN (1, 7)<br>);</pre><p><strong>Let’s break down the query step by step:</strong></p><ol><li>SELECT C.customer_name: This part of the query specifies the column you want to retrieve in the result set, which is &quot;customer_name.&quot;</li><li>FROM Customers C: This part of the query indicates that you are selecting data from the &quot;Customers&quot; table. The &quot;Customers&quot; table likely contains information about customers, including their customer names.</li><li>WHERE C.customer_id IN (...): This is the filtering condition in the query. It limits the results to only include customers who meet the specified criteria.</li><li>(SELECT O.customer_id FROM Orders O WHERE DAYOFWEEK(O.order_date) IN (1, 7)): This is a subquery that calculates the list of &quot;customer_id&quot; values for customers who have placed orders on either Sunday (1) or Saturday (7). Let&#39;s break down the subquery further:</li></ol><ul><li>SELECT O.customer_id: This part of the subquery selects the &quot;customer_id&quot; from the &quot;Orders&quot; table.</li><li>FROM Orders O: This part of the subquery specifies that it is retrieving data from the &quot;Orders&quot; table. The &quot;Orders&quot; table likely contains information about customer orders, including customer IDs and order dates.</li><li>WHERE DAYOFWEEK(O.order_date) IN (1, 7): This is the filtering condition within the subquery. It checks the day of the week for each order&#39;s &quot;order_date&quot; using the DAYOFWEEK function, and it selects orders where the day of the week is either 1 (Sunday) or 7 (Saturday).</li></ul><h4>9. Get the total order amount for each month.</h4><p><strong>Dataset:</strong> Orders (order_id, order_date, order_amount)</p><pre>SELECT DATE_FORMAT(order_date, &#39;%Y-%m&#39;) as month_year, SUM(order_amount) as total_order_amount<br>FROM Orders<br>GROUP BY month_year;</pre><p><strong>Let’s break down the query step by step:</strong></p><ol><li>SELECT DATE_FORMAT(order_date, &#39;%Y-%m&#39;) as month_year, SUM(order_amount) as total_order_amount: This part of the query specifies the columns you want to retrieve in the result set:</li></ol><ul><li>DATE_FORMAT(order_date, &#39;%Y-%m&#39;) as month_year: This column is derived from the &quot;order_date&quot; column and represents the month and year of each order date in the format &#39;YYYY-MM.&#39; It is aliased as &quot;month_year.&quot;</li><li>SUM(order_amount) as total_order_amount: This column calculates the total order amount for each group of orders within the same month-year. It is aliased as &quot;total_order_amount.&quot;</li></ul><ol><li>FROM Orders: This part of the query indicates that you are selecting data from the &quot;Orders&quot; table. The &quot;Orders&quot; table likely contains information about customer orders, including order dates and order amounts.</li><li>GROUP BY month_year: This clause groups the results by the &quot;month_year&quot; column, which is a formatted version of the &quot;order_date.&quot; This means that the aggregation function (SUM) will be applied to the order amounts within each month-year group.</li></ol><h4>10. Write a query to find the customers who have placed orders for more than two different products.</h4><p><strong>Dataset:</strong> Customers (customer_id, customer_name, Orders (order_id, customer_id, order_date), Order_Items (order_id, product_id, quantity)</p><pre>SELECT C.customer_id, C.customer_name<br>FROM Customers C<br>WHERE C.customer_id IN (<br>    SELECT O.customer_id<br>    FROM Orders O<br>    JOIN Order_Items OI ON O.order_id = OI.order_id<br>    GROUP BY O.customer_id<br>    HAVING COUNT(DISTINCT OI.product_id) &gt; 2<br>);</pre><p><strong>Let’s break down the query step by step:</strong></p><ol><li>SELECT C.customer_id, C.customer_name: This part of the query specifies the columns you want to retrieve in the result set, which are &quot;customer_id&quot; and &quot;customer_name.&quot;</li><li>FROM Customers C: This part of the query indicates that you are selecting data from the &quot;Customers&quot; table. The &quot;Customers&quot; table likely contains information about customers, including their customer IDs and names.</li><li>WHERE C.customer_id IN (...): This is the filtering condition in the query. It limits the results to only include customers who meet the specified criteria.</li><li>(SELECT O.customer_id FROM Orders O JOIN Order_Items OI ON O.order_id = OI.order_id GROUP BY O.customer_id HAVING COUNT(DISTINCT OI.product_id) &gt; 2): This is a subquery that calculates the list of &quot;customer_id&quot; values for customers who have placed orders for more than two distinct products. Let&#39;s break down the subquery further:</li></ol><ul><li>SELECT O.customer_id: This part of the subquery selects the &quot;customer_id&quot; from the &quot;Orders&quot; table.</li><li>FROM Orders O JOIN Order_Items OI ON O.order_id = OI.order_id: This part of the subquery performs two joins. It connects the &quot;Orders&quot; table to the &quot;Order_Items&quot; table using the &quot;order_id&quot; to link orders with their respective items.</li><li>GROUP BY O.customer_id: This clause groups the results in the subquery by &quot;customer_id.&quot; It means that the aggregation function (COUNT) will be applied to count the number of items for each customer.</li><li>HAVING COUNT(DISTINCT OI.product_id) &gt; 2: This is the filtering condition within the subquery. It filters the grouped results to only include customers who have ordered more than two distinct products, as it counts the number of unique product IDs in the &quot;Order_Items&quot; table.</li></ul><p>Happy querying!</p><p><em>I hope this was somehow useful to you, if it is say a thanks on my </em><a href="https://www.linkedin.com/in/pradeepchandra-reddy-s-c/"><em>Linkedin</em></a></p><p><strong><em>Thanks for reading! If you like the article make sure to:</em></strong></p><ul><li><strong>👏 Clap for the story (50 claps) and follow me</strong></li><li><strong>📰 View more content on my medium profile</strong></li></ul><p><strong><em>🔔 Follow Me On:</em></strong></p><p><a href="https://www.linkedin.com/in/pradeepchandra-reddy-s-c/">LinkedIn</a> | <a href="https://github.com/soopertramp">GitHub</a> | <a href="https://www.instagram.com/soopertramp07/">Instagram</a> | <a href="http://topmate.io/soopertramp">Topmate</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2d234ff514d7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Calculate Mode in SQL | How to Find Most Frequent Value in a Column?]]></title>
            <link>https://medium.com/@pradeepchandrareddy95/how-to-calculate-mode-in-sql-how-to-find-most-frequent-value-in-a-column-b3f913fdeb1e?source=rss-a668ce417d44------2</link>
            <guid isPermaLink="false">https://medium.com/p/b3f913fdeb1e</guid>
            <category><![CDATA[data]]></category>
            <category><![CDATA[database]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[problem-solving]]></category>
            <dc:creator><![CDATA[Pradeepchandra Reddy S C]]></dc:creator>
            <pubDate>Tue, 05 Sep 2023 09:03:39 GMT</pubDate>
            <atom:updated>2023-09-05T09:03:39.128Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tJqNMvjXt_k9FD6Y54YMxA.png" /><figcaption>Image</figcaption></figure><h4>Q 19 — <strong>How to Calculate Mode in SQL | How to Find Most Frequent Value in a Column?</strong></h4><p>To calculate the mode (most frequent value) in SQL, you can use the GROUP BY clause in combination with the COUNT function to count the occurrences of each unique value in a column.</p><p>Let’s say you have a table named sales with a column named product_category, and you want to find the mode (most frequent product category).</p><p><strong>Step 1:</strong> Count the occurrences of each unique value in the product_category column using the GROUP BY clause and COUNT function.</p><pre>SELECT product_category, COUNT(*) AS category_count<br>FROM sales<br>GROUP BY product_category</pre><p>This query will give you a result set that looks like this:</p><pre>+-----------------+---------------+<br>| product_category| category_count|<br>+-----------------+---------------+<br>| Electronics     | 25            |<br>| Clothing        | 15            |<br>| Furniture       | 30            |<br>| Books           | 20            |<br>+-----------------+---------------+</pre><p><strong>Step 2:</strong> Find the mode by selecting the row(s) with the highest count(s). You can use the ORDER BY clause to sort the result set by category_count in descending order and limit the result to the top row(s) using the LIMIT clause.</p><pre>SELECT product_category, COUNT(*) AS category_count<br>FROM sales<br>GROUP BY product_category<br>ORDER BY category_count DESC<br>LIMIT 1</pre><p>This query will give you the mode (most frequent product category) along with its count:</p><pre>+-----------------+---------------+<br>| product_category| category_count|<br>+-----------------+---------------+<br>| Furniture       | 30            |<br>+-----------------+---------------+</pre><p>In this example, &quot;Furniture&quot; is the mode as it appears most frequently in the product_category column with a count of 30.</p><p>This query will give you the mode (most frequent product category) along with its count.</p><p>If there are multiple values with the same highest count, the query will return all of them. You can adjust the LIMIT clause accordingly to retrieve the top N modes if needed (Or you can use CTE and RANK functions to circumvent the limits, explained below).</p><h4>Using CTE’s</h4><pre>WITH CategoryCounts AS (<br>    SELECT product_category, COUNT(*) AS category_count<br>    FROM sales<br>    GROUP BY product_category<br>)<br><br>SELECT product_category, category_count<br>FROM CategoryCounts<br>WHERE category_count = (<br>    SELECT MAX(category_count)<br>    FROM CategoryCounts<br>);</pre><p><strong>Here’s how this query works:</strong></p><ol><li>The first part of the query defines a CTE called CategoryCounts. This CTE calculates the counts for each unique value in the product_category column using the GROUP BY clause and COUNT function.</li><li>In the main query, we select the product_category and category_count from the CategoryCounts CTE.</li><li>We use a subquery within the WHERE clause to find the maximum count from the CTE using MAX(category_count).</li><li>The WHERE clause then filters the rows from the CTE where the category_count matches the maximum count, effectively giving us the mode(s).</li></ol><p>The result will include all the mode(s) along with their counts. If there are multiple values with the same highest count, they will all be included in the result.</p><h4>Using RANK</h4><pre>WITH CategoryCounts AS (<br>    SELECT product_category, COUNT(*) AS category_count<br>    FROM sales<br>    GROUP BY product_category<br>)<br><br>SELECT product_category, category_count<br>FROM (<br>    SELECT<br>        product_category,<br>        category_count,<br>        RANK() OVER (ORDER BY category_count DESC) AS ranking<br>    FROM CategoryCounts<br>) ranked_categories<br>WHERE ranking = 1;</pre><p><strong>Here’s how this query works:</strong></p><ol><li>The first part of the query defines a CTE called CategoryCounts. This CTE calculates the counts for each unique value in the product_category column using the GROUP BY clause and COUNT function.</li><li>In the main query, we use a subquery to rank the categories based on their counts in descending order using the RANK() window function. This assigns a rank of 1 to the most frequent category, 2 to the second most frequent, and so on.</li><li>We then select the product_category, category_count, and the rank from the subquery.</li><li>Finally, we filter the rows where the rank is equal to 1, effectively giving us the mode(s).</li></ol><p>This query using the RANK() function will return the mode(s) along with their counts. If there are multiple values with the same highest count, they will all be included in the result.</p><p>Using the RANK() function allows you to handle cases where there are ties for the most frequent value gracefully, providing a ranking for each value based on their frequency.</p><p><strong>Video for better explanation —</strong></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FS-rZgKIfiGE%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DS-rZgKIfiGE&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FS-rZgKIfiGE%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/b721b6defa1573e92e2f179087c3dee8/href">https://medium.com/media/b721b6defa1573e92e2f179087c3dee8/href</a></iframe><h4>Q 20 — Find the change in the employee status? (SQL Interview Question based on Full outer Join, asked in Deloitte)</h4><p><strong>Here is the ready script:</strong></p><pre>-- 2020 Employee Table<br><br>create table emp_2020<br>(<br>emp_id int,<br>designation varchar(20)<br>);<br><br>-- 2021 Employee Table<br><br>create table emp_2021<br>(<br>emp_id int,<br>designation varchar(20)<br>)<br><br>insert into emp_2020 values (1,&#39;Trainee&#39;), (2,&#39;Developer&#39;),(3,&#39;Senior Developer&#39;),(4,&#39;Manager&#39;);<br><br>insert into emp_2021 values (1,&#39;Developer&#39;), (2,&#39;Developer&#39;),(3,&#39;Manager&#39;),(5,&#39;Trainee&#39;);</pre><p>To find the change in employee status between the year 2020 and 2021 based on the provided emp_2020 and emp_2021 tables, you can use a SQL query with a full outer join. Here&#39;s the SQL query to achieve this:</p><pre>SELECT<br>    COALESCE(e20.emp_id, e21.emp_id) AS emp_id,<br>    e20.designation AS designation_2020,<br>    e21.designation AS designation_2021,<br>    CASE<br>        WHEN e20.designation IS NULL THEN &#39;New Joinee&#39;<br>        WHEN e21.designation IS NULL THEN &#39;Resigned&#39;<br>        WHEN e20.designation &lt;&gt; e21.designation THEN &#39;Promoted&#39;<br>    END AS status<br>FROM<br>    emp_2020 e20<br>FULL OUTER JOIN<br>    emp_2021 e21<br>ON<br>    e20.emp_id = e21.emp_id<br>WHERE<br>    (e20.designation IS NULL OR e21.designation IS NULL)<br>    OR e20.designation &lt;&gt; e21.designation;</pre><p><strong>Output -</strong></p><pre>+-------+-----------------+-----------------+-------------+<br>| emp_id| designation_2020| designation_2021|   status    |<br>+-------+-----------------+-----------------+-------------+<br>|   1   |     Trainee     |     Developer   |  Promoted   |<br>|   3   | Senior Developer|     Manager     |  Promoted   |<br>|   4   |     Manager     |       --        |  Resigned   |<br>|   5   |       --        |     Trainee     |  New Joinee |<br>+-------+-----------------+-----------------+-------------+</pre><p><strong>Explanation:</strong></p><ol><li>The query starts with a SELECT statement, which will return a result set with the following columns:</li></ol><ul><li>emp_id: This column is determined using the COALESCE function, which returns the first non-null value from the given expressions. It tries to get emp_id from both the emp_2020 (aliased as e20) and emp_2021 (aliased as e21) tables. If emp_id is null in one table but not in the other, it will select the non-null value.</li><li>designation_2020: This column represents the designation of the employee in the year 2020, taken from the e20 table.</li><li>designation_2021: This column represents the designation of the employee in the year 2021, taken from the e21 table.</li><li>status: This column uses a CASE statement to determine the status of each employee based on the following conditions:</li><li>If the employee’s designation in 2020 is null, it is considered a ‘New Joinee.’</li><li>If the employee’s designation in 2021 is null, it is considered ‘Resigned.’</li><li>If the employee’s designation in 2020 and 2021 are not null but different, it is considered ‘Changed.’</li></ul><p>2. The FROM clause specifies that the data is being retrieved from two tables, emp_2020 (aliased as e20) and emp_2021 (aliased as e21), using a FULL OUTER JOIN. This means that all records from both tables are included in the result set, and where there is no match between emp_id in the two tables, the columns for the missing side will contain null values.</p><p>3. The ON clause defines the join condition, where e20.emp_id is matched with e21.emp_id. This is how the two tables are related.</p><p>4. The WHERE clause filters the result set. It includes records in the result set where:</p><ul><li>The designation in 2020 (e20.designation) is null or</li><li>The designation in 2021 (e21.designation) is null or</li><li>The designation in 2020 and 2021 are not the same.</li></ul><p>In summary, this query combines data from two employee tables for 2020 and 2021, categorizes employees based on their designation changes, and includes cases where employees joined or resigned. The result set will show the emp_id, designations for both years, and the status of each employee for the specified conditions.</p><p><strong>Video Link for Understanding Better —</strong></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FKQfWd6V3IB8%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DKQfWd6V3IB8&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FKQfWd6V3IB8%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/0ce3886a8bab3d7633de48521c09bc06/href">https://medium.com/media/0ce3886a8bab3d7633de48521c09bc06/href</a></iframe><p>Happy querying!</p><p><em>I hope this was somehow useful to you, if it is say a thanks on my </em><a href="https://www.linkedin.com/in/pradeepchandra-reddy-s-c/"><em>Linkedin</em></a></p><p><strong><em>Thanks for reading! If you like the article make sure to:</em></strong></p><ul><li><strong>👏 Clap for the story (50 claps) and follow me</strong></li><li><strong>📰 View more content on my medium profile</strong></li></ul><p><strong><em>🔔 Follow Me On:</em></strong></p><p><a href="https://www.linkedin.com/in/pradeepchandra-reddy-s-c/">LinkedIn</a> | <a href="https://github.com/soopertramp">GitHub</a> | <a href="https://www.instagram.com/soopertramp07/">Instagram</a> | <a href="http://topmate.io/soopertramp">Topmate</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b3f913fdeb1e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mastering SQL JOINs: A Comprehensive Guide for Job Interviews]]></title>
            <link>https://medium.com/@pradeepchandrareddy95/mastering-sql-joins-a-comprehensive-guide-for-job-interviews-468605995082?source=rss-a668ce417d44------2</link>
            <guid isPermaLink="false">https://medium.com/p/468605995082</guid>
            <category><![CDATA[data]]></category>
            <category><![CDATA[interview]]></category>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[database]]></category>
            <dc:creator><![CDATA[Pradeepchandra Reddy S C]]></dc:creator>
            <pubDate>Mon, 04 Sep 2023 06:13:56 GMT</pubDate>
            <atom:updated>2023-09-04T06:13:56.411Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tJqNMvjXt_k9FD6Y54YMxA.png" /><figcaption>Image</figcaption></figure><h4>How many records are in the output when you perform different types of JOINs on two tables?</h4><p>SQL JOIN operations are a common topic in interviews, and you may be asked to determine the number of records in the output when you perform different types of JOINs on two tables. Here are four common types of JOINs (INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN).</p><p>Before that Let’s assume we have two tables: Employees and Departments, and they have the following structures:</p><p><strong>Employees Table:</strong></p><pre>| EmployeeID | EmployeeName | DepartmentID |<br>|------------|--------------|-------------|<br>| 1          | Alice        | 1           |<br>| 2          | Bob          | 2           |<br>| 3          | Charlie      | 1           |<br>| 4          | David        | 3           |</pre><p><strong>Departments Table:</strong></p><pre>| DepartmentID | DepartmentName |<br>|--------------|----------------|<br>| 1            | HR             |<br>| 2            | IT             </pre><p><strong>INNER JOIN:</strong></p><ul><li>An INNER JOIN returns only the rows where there is a match in both tables.</li><li>Number of records in the output: 2</li><li>Example SQL Query:</li></ul><pre>SELECT <br>    Employees.EmployeeName, <br>    Departments.DepartmentName<br>FROM <br>    Employees<br>    INNER JOIN Departments <br>    ON Employees.DepartmentID = Departments.DepartmentID;</pre><pre>Output:<br><br>| EmployeeName | DepartmentName |<br>|--------------|----------------|<br>| Alice        | HR             |<br>| Bob          | IT             |</pre><p><strong>LEFT JOIN (or LEFT OUTER JOIN)</strong>:</p><ul><li>A LEFT JOIN returns all the rows from the left table and the matched rows from the right table. If there is no match, NULL values are returned for the right table’s columns.</li><li>Number of records in the output: 4</li><li>Example SQL Query:</li></ul><pre>SELECT <br>    Employees.EmployeeName, <br>    Departments.DepartmentName<br>FROM <br>    Employees<br>    LEFT JOIN Departments <br>    ON Employees.DepartmentID = Departments.DepartmentID;</pre><pre>Output:<br><br>| EmployeeName | DepartmentName |<br>|--------------|----------------|<br>| Alice        | HR             |<br>| Bob          | IT             |<br>| Charlie      | HR             |<br>| David        | NULL           |</pre><p><strong>RIGHT JOIN (or RIGHT OUTER JOIN):</strong></p><ul><li>A RIGHT JOIN returns all the rows from the right table and the matched rows from the left table. If there is no match, NULL values are returned for the left table’s columns.</li><li>Number of records in the output: 3</li><li>Example SQL Query:</li></ul><pre>SELECT <br>    Employees.EmployeeName, <br>    Departments.DepartmentName<br>FROM <br>    Employees<br>    RIGHT JOIN Departments <br>    ON Employees.DepartmentID = Departments.DepartmentID;</pre><pre>Output:<br><br>| EmployeeName | DepartmentName |<br>|--------------|----------------|<br>| Alice        | HR             |<br>| Bob          | IT             |<br>| NULL         | NULL           |</pre><p><strong>FULL OUTER JOIN:</strong></p><ul><li>A FULL OUTER JOIN returns all rows when there is a match in either the left or right table. If there is no match, NULL values are returned for the missing side’s columns.</li><li>Number of records in the output: 5</li><li>Example SQL Query:</li></ul><pre>SELECT <br>    Employees.EmployeeName, <br>    Departments.DepartmentName<br>FROM <br>    Employees<br>    FULL OUTER JOIN Departments <br>    ON Employees.DepartmentID = Departments.DepartmentID;</pre><pre>Output:<br><br>| EmployeeName | DepartmentName |<br>|--------------|----------------|<br>| Alice        | HR             |<br>| Bob          | IT             |<br>| Charlie      | HR             |<br>| David        | NULL           |<br>| NULL         | NULL           |</pre><p>Understanding these JOIN operations is essential for acing your SQL interviews. Make sure to practice and explain your results confidently!</p><p><strong>Video Link for Understanding Better —</strong></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FxR87ctOgpAE%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DxR87ctOgpAE&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FxR87ctOgpAE%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/cbccd72449aca912db9bf4134fbc1d0f/href">https://medium.com/media/cbccd72449aca912db9bf4134fbc1d0f/href</a></iframe><p>Happy querying!</p><p><em>I hope this was somehow useful to you, if it is say a thanks on my </em><a href="https://www.linkedin.com/in/pradeepchandra-reddy-s-c/"><em>Linkedin</em></a></p><p><strong><em>Thanks for reading! If you like the article make sure to:</em></strong></p><ul><li><strong>👏 Clap for the story (50 claps) and follow me</strong></li><li><strong>📰 View more content on my medium profile</strong></li></ul><p><strong><em>🔔 Follow Me On:</em></strong></p><p><a href="https://www.linkedin.com/in/pradeepchandra-reddy-s-c/">LinkedIn</a> | <a href="https://github.com/soopertramp">GitHub</a> | <a href="https://www.instagram.com/soopertramp07/">Instagram</a> | <a href="http://topmate.io/soopertramp">Topmate</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=468605995082" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Practice SQL without creating tables in your database?]]></title>
            <link>https://medium.com/@pradeepchandrareddy95/how-to-practice-sql-without-creating-tables-in-your-database-6b3997b20cb4?source=rss-a668ce417d44------2</link>
            <guid isPermaLink="false">https://medium.com/p/6b3997b20cb4</guid>
            <category><![CDATA[data]]></category>
            <category><![CDATA[tips-and-tricks]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[database]]></category>
            <category><![CDATA[sql]]></category>
            <dc:creator><![CDATA[Pradeepchandra Reddy S C]]></dc:creator>
            <pubDate>Sun, 03 Sep 2023 11:37:11 GMT</pubDate>
            <atom:updated>2023-09-03T11:55:45.653Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to Practice SQL without creating tables in your database</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9F2h9GsMzGHXU-DIfEY-Ww.png" /><figcaption>Image</figcaption></figure><p>Learning SQL is an essential skill for anyone working with databases or data analysis. Traditionally, practicing SQL involved creating physical tables in your database, which can be cumbersome and risky, especially in a production environment.</p><p>However, there’s a better way to hone your SQL skills without the need for physical tables — by using Common Table Expressions (CTEs). In this article, we’ll explore how to practice SQL effectively using CTEs, without creating any permanent data structures in your database.</p><ol><li>Identifying the Data Requirements: Before you start practicing SQL with CTEs, it’s crucial to determine the kind of data you need for your practice. Think about the structure and content of the tables you would typically use in your database. Understanding your data requirements will guide you in creating relevant CTEs.</li><li>Creating CTEs: Instead of creating physical tables, you can create CTEs to simulate the data you need. CTEs are temporary result sets defined within your SQL query, and they exist only for the duration of that query.</li></ol><p>Here’s an example of creating CTEs for employees and departments:</p><pre>WITH Employees AS (<br>    SELECT 1 AS EmployeeID, &#39;Rajesh&#39; AS FirstName, &#39;Kumar&#39; AS LastName, &#39;Sales&#39; AS Department<br>    UNION ALL<br>    SELECT 2, &#39;Priya&#39;, &#39;Patel&#39;, &#39;HR&#39;<br>    UNION ALL<br>    SELECT 3, &#39;Amit&#39;, &#39;Sharma&#39;, &#39;IT&#39;<br>),<br>Departments AS (<br>    SELECT &#39;Sales&#39; AS DepartmentName, &#39;Mumbai&#39; AS Location<br>    UNION ALL<br>    SELECT &#39;HR&#39;, &#39;Delhi&#39;<br>    UNION ALL<br>    SELECT &#39;IT&#39;, &#39;Bangalore&#39;<br>)<br>SELECT * FROM Employees;<br>SELECT * FROM Departments;</pre><p>3. Writing Queries: With your CTEs in place, you can now write SQL queries just as you would with physical tables. You can join, filter, and manipulate the data within these CTEs. Here’s an example of querying data from the previously created CTEs:</p><pre>SELECT e.FirstName, e.LastName, d.Location<br>FROM Employees e<br>JOIN Departments d ON e.Department = d.DepartmentName;</pre><p>4. Practice and Experiment: You can run these queries in your SQL environment to practice and experiment with SQL without affecting your actual database tables. This approach provides a safe and flexible way to enhance your SQL skills.</p><p>5. Disposal of CTEs: It’s essential to remember that CTEs are temporary and exist only for the duration of the query execution. Once you close the query, the CTEs are automatically removed. You don’t need to worry about cleaning up or dropping these temporary structures, making this method efficient and hassle-free.</p><p>Using Common Table Expressions (CTEs) for practice is a powerful technique that allows you to develop your SQL skills without creating physical tables.</p><p>By understanding your data requirements, creating relevant CTEs, and writing queries, you can gain hands-on experience and confidence in SQL. So, start practicing with CTEs today and take your SQL skills to the next level.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FZAY2oncnmBQ%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DZAY2oncnmBQ&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FZAY2oncnmBQ%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/00395277697f11468328e7179f167498/href">https://medium.com/media/00395277697f11468328e7179f167498/href</a></iframe><p>Happy querying!</p><p><em>I hope this was somehow useful to you, if it is say a thanks on my </em><a href="https://www.linkedin.com/in/pradeepchandra-reddy-s-c/"><em>Linkedin</em></a></p><p><strong><em>Thanks for reading! If you like the article make sure to:</em></strong></p><ul><li><strong>👏 Clap for the story (50 claps) and follow me</strong></li><li><strong>📰 View more content on my medium profile</strong></li></ul><p><strong><em>🔔 Follow Me On:</em></strong></p><p><a href="https://www.linkedin.com/in/pradeepchandra-reddy-s-c/">LinkedIn</a> | <a href="https://github.com/soopertramp">GitHub</a> | <a href="https://www.instagram.com/soopertramp07/">Instagram</a> | <a href="http://topmate.io/soopertramp">Topmate</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6b3997b20cb4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding SELF Join and CROSS Join]]></title>
            <link>https://medium.com/@pradeepchandrareddy95/understanding-self-join-and-cross-join-2bb0c0bd40c2?source=rss-a668ce417d44------2</link>
            <guid isPermaLink="false">https://medium.com/p/2bb0c0bd40c2</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[data]]></category>
            <category><![CDATA[database]]></category>
            <category><![CDATA[analytics]]></category>
            <category><![CDATA[sql]]></category>
            <dc:creator><![CDATA[Pradeepchandra Reddy S C]]></dc:creator>
            <pubDate>Sat, 02 Sep 2023 05:23:43 GMT</pubDate>
            <atom:updated>2023-09-02T05:23:43.695Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tJqNMvjXt_k9FD6Y54YMxA.png" /></figure><h4>SELF Join</h4><p><strong>A self-join</strong> is a specific type of join in SQL where a table is joined with itself. In other words, you treat a single table as if it were two separate tables, and then you perform a join operation between these “virtual” tables within the same table.</p><p><strong>Self-joins can be particularly useful in scenarios like:</strong></p><p><strong>Hierarchical Data:</strong> For example, when you have an organizational chart with employees and their managers in the same table, and you want to retrieve information about employees and their respective managers.</p><p><strong>Recursive Data: </strong>When dealing with recursive data structures like comments on a post, where comments can have replies (which are also comments), a self-join can help retrieve the hierarchical structure.</p><p><strong>Comparing Data within the Same Table:</strong> When you need to compare values within the same table, such as finding employees with salaries greater than their managers’ salaries.</p><h4><strong>Q — Find employees with salary more than their mangers salary</strong></h4><pre>CREATE TABLE emp_manager (<br>    emp_id INT,<br>    emp_name VARCHAR(50),<br>    salary INT,<br>    manager_id INT<br>);<br><br>INSERT INTO emp_manager VALUES<br>(1, &#39;Ankit&#39;, 10000, 4),<br>(2, &#39;Mohit&#39;, 15000, 5),<br>(3, &#39;Vikas&#39;, 10000, 4),<br>(4, &#39;Rohit&#39;, 5000, 2),<br>(5, &#39;Mudit&#39;, 12000, 6),<br>(6, &#39;Agam&#39;, 12000, 2),<br>(7, &#39;Sanjay&#39;, 9000, 2),<br>(8, &#39;Ashish&#39;, 5000, 2);</pre><p>This code creates the emp_manager table with the specified columns and inserts eight rows of sample data into it. The data represents employees, their names, salaries, and the manager_id that associates them with their respective managers.</p><pre>SELECT e.emp_id, e.emp_name, e.salary<br>FROM emp_manager e<br>INNER JOIN emp_manager m ON e.manager_id = m.emp_id<br>WHERE e.salary &gt; m.salary;</pre><ol><li>We perform a self-join on the emp_manager table, using aliases e for the employees and m for their managers.</li><li>We establish the join condition with e.manager_id = m.emp_id, which links employees to their respective managers.</li><li>The WHERE clause filters the results to include only rows where an employee&#39;s salary (e.salary) is greater than their manager&#39;s salary (m.salary).</li></ol><p>Running this query on the sample data given above will give you a list of employees who earn more than their managers.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FG7v7TZ3ylDI%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DG7v7TZ3ylDI&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FG7v7TZ3ylDI%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/6f383e33279404a98c921470e2d49c67/href">https://medium.com/media/6f383e33279404a98c921470e2d49c67/href</a></iframe><h4>CROSS Join</h4><p>A cross join, also known as a Cartesian join, is a type of join operation in SQL where each row from one table is combined with every row from another table.</p><p>In essence, it forms the Cartesian product of the two tables, resulting in a large number of rows equal to the product of the number of rows in each table involved in the join.</p><p>Unlike other types of joins (e.g., INNER JOIN, LEFT JOIN), there is no specific condition specified for matching rows in a cross join. Instead, it simply combines all possible combinations of rows.</p><p><strong>Here are some scenarios where cross joins can be useful:</strong></p><ol><li><strong>Creating Combinations</strong>: Cross joins are often used to create combinations of data. For example, if you have a table of colors and a table of sizes, you can cross join them to generate all possible combinations of colors and sizes.</li><li><strong>Generating Test Data</strong>: Cross joins can be helpful for generating test data or sample data for testing purposes, especially when you need a large dataset with various combinations.</li><li><strong>Creating Pivot Tables</strong>: In some cases, you might need to create pivot tables or matrices where you want to combine rows and columns to form a matrix of data.</li></ol><h4>Example Questions to Solve —</h4><p><strong>Products Table —</strong></p><pre>CREATE TABLE products (<br>    id INT,<br>    name VARCHAR(10)<br>);<br><br>INSERT INTO products VALUES <br>    (1, &#39;A&#39;),<br>    (2, &#39;B&#39;),<br>    (3, &#39;C&#39;),<br>    (4, &#39;D&#39;),<br>    (5, &#39;E&#39;);</pre><p><strong>Colors Table -</strong></p><pre>CREATE TABLE colors (<br>    color_id INT,<br>    color VARCHAR(50)<br>);<br><br>INSERT INTO colors VALUES <br>    (1, &#39;Blue&#39;),<br>    (2, &#39;Green&#39;),<br>    (3, &#39;Orange&#39;);</pre><p><strong>Sizes Table —</strong></p><pre>CREATE TABLE sizes (<br>    size_id INT,<br>    size VARCHAR(10)<br>);<br><br>INSERT INTO sizes VALUES <br>    (1, &#39;M&#39;),<br>    (2, &#39;L&#39;),<br>    (3, &#39;XL&#39;);</pre><p><strong>Transactions Table —</strong></p><pre>CREATE TABLE transactions (<br>    order_id INT,<br>    product_name VARCHAR(20),<br>    color VARCHAR(10),<br>    size VARCHAR(10),<br>    amount INT<br>);<br><br>INSERT INTO transactions VALUES <br>    (1, &#39;A&#39;, &#39;Blue&#39;, &#39;L&#39;, 300),<br>    (2, &#39;B&#39;, &#39;Blue&#39;, &#39;XL&#39;, 150),<br>    (3, &#39;B&#39;, &#39;Green&#39;, &#39;L&#39;, 250),<br>    (4, &#39;C&#39;, &#39;Blue&#39;, &#39;L&#39;, 250),<br>    (5, &#39;E&#39;, &#39;Green&#39;, &#39;L&#39;, 270),<br>    (6, &#39;D&#39;, &#39;Orange&#39;, &#39;L&#39;, 200),<br>    (7, &#39;D&#39;, &#39;Green&#39;, &#39;M&#39;, 250);</pre><h4>Q 1: Retrieve a list of all possible combinations of products, colors, and sizes, regardless of any transactions (Creates Master Data)</h4><pre>SELECT<br>    p.name AS product_name,<br>    c.color AS product_color,<br>    s.size AS product_size<br>FROM<br>    products p<br>CROSS JOIN<br>    colors c<br>CROSS JOIN<br>    sizes s;</pre><p>The query starts by selecting specific columns from the products, colors, and sizes tables and assigns aliases to those columns. Then, it uses CROSS JOIN operations to generate all possible combinations of these columns from the respective tables.</p><p>The result will be a very large dataset containing every product name, color, and size combination.</p><h4>Q 2: Calculate the total amount sold for each combination of product, color, and size.</h4><pre>SELECT<br>    p.name AS product_name,<br>    c.color AS product_color,<br>    s.size AS product_size,<br>    COALESCE(SUM(t.amount), 0) AS total_amount_sold<br>FROM<br>    products p<br>CROSS JOIN<br>    colors c<br>CROSS JOIN<br>    sizes s<br>LEFT JOIN<br>    transactions t ON p.name = t.product_name AND c.color = t.color AND s.size = t.size<br>GROUP BY<br>    p.name,<br>    c.color,<br>    s.size;</pre><p>This SQL query combines information about products, their colors, and sizes to calculate the total amount sold for each product-color-size combination. It achieves this through the following steps:</p><ol><li>It generates all possible combinations of products, colors, and sizes using CROSS JOIN.</li><li>It then left joins these combinations with transaction data to find sales amounts.</li><li>For each combination, it calculates the total sales amount, using SUM to add up transaction amounts, and COALESCE to handle cases where there are no transactions, setting the total to 0.</li><li>Finally, it groups the results by product name, color, and size.</li></ol><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FG-5Mm8ezt3I%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DG-5Mm8ezt3I&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FG-5Mm8ezt3I%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/53c469d1e006961cdf7799dfc0637187/href">https://medium.com/media/53c469d1e006961cdf7799dfc0637187/href</a></iframe><p><em>I hope this was somehow useful to you, if it is say a thanks on my </em><a href="https://www.linkedin.com/in/pradeepchandra-reddy-s-c/"><em>Linkedin</em></a></p><p><strong><em>Thanks for reading! If you like the article make sure to:</em></strong></p><ul><li><strong>👏 Clap for the story (50 claps) and follow me</strong></li><li><strong>📰 View more content on my medium profile</strong></li></ul><p><strong><em>🔔 Follow Me On:</em></strong></p><p><a href="https://www.linkedin.com/in/pradeepchandra-reddy-s-c/">LinkedIn</a> | <a href="https://github.com/soopertramp">GitHub</a> | <a href="https://www.instagram.com/soopertramp07/">Instagram</a> | <a href="http://topmate.io/soopertramp">Topmate</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2bb0c0bd40c2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Top 10 (Frequently asked) SQL Interview Questions]]></title>
            <link>https://medium.com/@pradeepchandrareddy95/top-10-frequently-asked-sql-interview-questions-f15a967c8656?source=rss-a668ce417d44------2</link>
            <guid isPermaLink="false">https://medium.com/p/f15a967c8656</guid>
            <category><![CDATA[database]]></category>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[analytics]]></category>
            <category><![CDATA[data]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[Pradeepchandra Reddy S C]]></dc:creator>
            <pubDate>Fri, 01 Sep 2023 07:15:01 GMT</pubDate>
            <atom:updated>2023-09-01T11:45:33.275Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tJqNMvjXt_k9FD6Y54YMxA.png" /></figure><p>These are the most frequently asked top 10 SQL interview questions. If you have any interview I am sure 2–3 questions will be from this list.</p><h4>First let’s see the table script :</h4><p>Employee Table -</p><pre>CREATE TABLE emp (<br>  emp_id INT,<br>  emp_name VARCHAR(20),<br>  department_id INT,<br>  salary INT,<br>  manager_id INT,<br>  emp_age INT<br>);<br><br>INSERT INTO emp VALUES (1, &#39;Ankit&#39;, 100,10000, 4, 39);<br>INSERT INTO emp VALUES (2, &#39;Mohit&#39;, 100, 15000, 5, 48);<br>INSERT INTO emp VALUES (3, &#39;Vikas&#39;, 100, 10000,4,37);<br>INSERT INTO emp VALUES (4, &#39;Rohit&#39;, 100, 5000, 2, 16);<br>INSERT INTO emp VALUES (5, &#39;Mudit&#39;, 200, 12000, 6,55);<br>INSERT INTO emp VALUES (6, &#39;Agam&#39;, 200, 12000,2, 14);<br>INSERT INTO emp VALUES (7, &#39;Sanjay&#39;, 200, 9000, 2,13);<br>INSERT INTO emp VALUES (8, &#39;Ashish&#39;, 200,5000,2,12);<br>INSERT INTO emp VALUES (9, &#39;Mukesh&#39;,300,6000,6,51);<br>INSERT INTO emp VALUES (10, &#39;Rakesh&#39;,300,7000,6,50);</pre><p>Departments Table -</p><pre>CREATE TABLE department(<br> <br> dept_id INT,<br> dept_name VARCHAR(10)<br> );<br> <br>INSERT INTO department values(100,&#39;Analytics&#39;);<br>INSERT INTO department values(300,&#39;IT&#39;);</pre><p>Orders Table -</p><pre>CREATE TABLE orders(<br> customer_name CHAR(10),<br> order_date DATE,<br> order_amount INT,<br> customer_gender CHAR(6)<br> );<br> <br>INSERT INTO orders values(&#39;Shilpa&#39;,&#39;2020-01-01&#39;,10000,&#39;Female&#39;);<br>INSERT INTO orders values(&#39;Rahul&#39;,&#39;2020-01-02&#39;,12000,&#39;Male&#39;);<br>INSERT INTO orders values(&#39;PRADEEP&#39;,&#39;2020-01-02&#39;,12000,&#39;Female&#39;);<br>INSERT INTO orders values(&#39;Rohit&#39;,&#39;2020-01-03&#39;,15000,&#39;Male&#39;);<br>INSERT INTO orders values(&#39;pradeep&#39;,&#39;2020-01-03&#39;,14000,&#39;Female&#39;);<br> <br>INSERT INTO department values(100,&#39;Analytics&#39;);<br>INSERT INTO department values(300,&#39;IT&#39;);</pre><h4>Let’s get into Questions now -</h4><h4>Q 1 — How to find duplicates in a given table?</h4><pre>SELECT <br>    emp_id, COUNT(*)<br>FROM<br>    emp<br>GROUP BY emp_id<br>HAVING COUNT(*) &gt; 1;</pre><ul><li>SELECT emp_id, COUNT(*): This part selects the emp_id column and calculates the count of rows for each emp_id.</li><li>FROM emp: Specifies that you&#39;re querying the emp table.</li><li>GROUP BY emp_id: Groups the rows in the emp table by the emp_id column, so you get one row for each unique emp_id.</li><li>HAVING COUNT(*) &gt; 1: This is a filter condition that ensures only rows with a count greater than 1 (i.e., duplicates) are included in the result.</li></ul><p>So, the result of this query will be a list of emp_id values that appear more than once in the emp table along with their respective counts.</p><h4>Q 2 — How to delete duplicates?</h4><pre>WITH cte AS (<br>    SELECT *, ROW_NUMBER() OVER (PARTITION BY emp_id ORDER BY emp_id) AS rn <br>    FROM emp<br>)<br>DELETE FROM cte WHERE rn &gt; 1;</pre><ul><li>WITH cte AS (...): This defines a Common Table Expression (CTE) named cte. The CTE selects all columns from the emp table and adds a new column rn using the ROW_NUMBER() window function. The PARTITION BY emp_id clause means that the row numbers are assigned separately for each unique emp_id, and the ORDER BY emp_id clause orders the rows within each partition by emp_id.</li><li>DELETE FROM cte WHERE rn &gt; 1;: This statement deletes rows from the CTE cte where the row number (rn) is greater than 1. Since the ROW_NUMBER() function assigns a value of 1 to the first occurrence of each emp_id and increments it for subsequent duplicates, this effectively deletes all but one instance of each duplicate emp_id.</li></ul><p>After running this SQL code, the emp table will be left with only one instance of each unique emp_id, and duplicates will be removed.</p><p>Please make sure to have a backup or understand the implications of such deletions before running this kind of operation in a production environment.</p><h4>Q 3 — Difference between union and union all?</h4><p>The UNION and UNION ALL operators in SQL are used to combine the result sets of two or more SELECT statements into a single result set. However, they differ in how they handle duplicate rows:</p><ol><li><strong>UNION:</strong></li></ol><ul><li>The UNION operator removes duplicate rows from the combined result set.</li><li>It returns only distinct (unique) rows from the result sets of the individual SELECT statements.</li><li>The result set will contain only one occurrence of each distinct row.</li></ul><p>For example, if you have duplicate rows in both SELECT statements, UNION will remove those duplicates from the final result.</p><pre>SELECT * FROM emp <br>UNION <br>SELECT * FROM emp;</pre><p>This query will return a result set with only distinct rows.</p><p><strong>2. UNION ALL:</strong></p><ul><li>The UNION ALL operator does not remove duplicate rows; it includes all rows from the result sets of the individual SELECT statements, including duplicates.</li><li>It preserves all rows from the individual SELECT statements, even if they are duplicates.</li></ul><p>For example, if you have duplicate rows in both SELECT statements, UNION ALL will include all occurrences of those rows in the final result.</p><pre>SELECT * FROM emp <br>UNION ALL <br>SELECT * FROM emp;</pre><p>This query will return a result set that includes all rows, including duplicates.</p><p>In summary, the main difference between UNION and UNION ALL is that UNION removes duplicate rows, while UNION ALL retains all rows, including duplicates, from the individual SELECT statements. The choice between them depends on whether you want to eliminate duplicates or include all rows, even if they are duplicates, in your result set.</p><h4>Q 4 — Difference between Rank, Row number, Dense Rank?</h4><p>In SQL, RANK(), ROW_NUMBER(), and DENSE_RANK() are window functions used to assign a rank or row number to rows in a result set based on the specified ordering. However, they have different behaviors when it comes to handling ties (rows with the same values). Let&#39;s go through each of them:</p><p><strong>RANK():</strong></p><ul><li>RANK() assigns a unique rank to each distinct row in the result set.</li><li>If multiple rows have the same values and are assigned the same rank, the next rank will skip the number of tied rows and continue from there. In other words, it leaves gaps in the ranking sequence for tied rows.</li><li>It assigns the same rank to tied rows but doesn’t provide unique row numbers.</li></ul><pre>SELECT <br>       emp_id, <br>       emp_name, <br>       salary, <br>       RANK() OVER(ORDER BY salary DESC) AS rnk <br>FROM <br>       emp;</pre><p>The query assigns ranks based on salary in descending order using RANK(). If multiple employees have the same salary, they will receive the same rank, and the next rank will skip the number of tied employees.</p><p><strong>ROW_NUMBER():</strong></p><ul><li>ROW_NUMBER() assigns a unique row number to each row in the result set.</li><li>It doesn’t consider ties. If multiple rows have the same values, they will still receive unique row numbers.</li><li>It doesn’t leave gaps in the row numbering sequence.</li></ul><pre>SELECT <br>      emp_id, <br>      emp_name, <br>      salary, <br>      ROW_NUMBER() OVER(ORDER BY salary DESC) AS rn <br>FROM <br>      emp;</pre><p>The query assigns row numbers based on salary in descending order using ROW_NUMBER(). It assigns a unique row number to each employee, regardless of ties in salary.</p><p><strong>DENSE_RANK():</strong></p><ul><li>DENSE_RANK() assigns a unique rank to each distinct row in the result set.</li><li>If multiple rows have the same values, they are assigned the same rank, and the next rank does not skip any values. It provides a dense ranking without gaps for tied rows.</li><li>It assigns the same rank to tied rows but doesn’t provide unique row numbers.</li></ul><pre>SELECT <br>      emp_id, <br>      emp_name, <br>      salary, <br>      DENSE_RANK() OVER(ORDER BY salary DESC) AS rn <br>FROM <br>      emp;</pre><p>The query assigns dense ranks based on salary in descending order using DENSE_RANK(). If multiple employees have the same salary, they will receive the same dense rank, and the next dense rank will not skip any values.</p><h4>Q 5 — Employees who are not present in department table?</h4><pre>SELECT <br>    *<br>FROM<br>    emp AS e<br>        LEFT JOIN<br>    department AS d ON e.department_id = d.dept_id<br>WHERE d.dept_id IS NULL;</pre><ol><li>SELECT *: This part of the query selects all columns (*) from the result set.</li><li>FROM emp AS e: Specifies the source table for the query. It aliases the emp table as e, which means you can refer to it as e in the rest of the query.</li><li>LEFT JOIN: This is a type of join that combines rows from the left table (emp) with rows from the right table (department) based on a common column (department_id in emp and dept_id in department). It includes all rows from the left table, and only the matching rows from the right table. If there&#39;s no match in the right table, it includes NULL values for columns from the right table.</li><li>department AS d: Specifies the right table for the join and aliases it as d, allowing you to refer to it as d in the query.</li><li>ON e.department_id = d.dept_id: This part of the query defines the join condition, specifying that rows from the emp table (e) should be matched with rows from the department table (d) based on the department_id and dept_id columns, respectively.</li><li>WHERE d.dept_id IS NULL;: This is the filter condition. It specifies that you want to filter the result set to only include rows where the dept_id column from the right table (d, which is the department table) is NULL. This condition effectively selects employees (e) who do not have a matching department in the department table.</li></ol><p>So, the overall result of this query will be a list of all columns for employees (emp) who are not associated with any department (where dept_id is NULL in the department table). These are the employees who are not present in the department table.</p><h4>Q 6 — Second highest salary in each department?</h4><pre>SELECT *<br>FROM (<br>    SELECT <br>        emp_name, <br>        salary, <br>        department_id, <br>        DENSE_RANK() OVER (PARTITION BY department_id ORDER BY salary DESC) AS rn<br>    FROM emp<br>) AS a<br>WHERE rn = 2;</pre><ol><li>The inner query selects the emp_name, salary, and department_id columns from the emp table.</li><li>The DENSE_RANK() function is used within the inner query. It assigns a dense rank to each employee within their respective departments based on their salaries. The PARTITION BY clause divides the result set into partitions, one for each unique department_id, and the ORDER BY clause orders the rows within each partition by salary in descending order.</li><li>The result of the inner query is aliased as a.</li><li>The outer query selects all columns (*) from the result of the inner query (a).</li><li>The WHERE clause filters the result to include only rows where the dense rank (rn) is equal to 2. This means it selects employees who have the second-highest salary within their respective departments.</li></ol><p>So, the final result will be a list of employees with the second-highest salary in each department.</p><h4>Q — 7 : Find all the transactions done by customer named Pradeep.</h4><pre>SELECT *<br>FROM orders<br>WHERE UPPER(customer_name) = &#39;PRADEEP&#39;;</pre><ol><li>SELECT *: This part of the query specifies that you want to select all columns (*) from the orders table in the result set. It means you want to retrieve all the details of the transactions.</li><li>FROM orders: This clause indicates the source table for the query, which is the orders table. The query will retrieve data from this table.</li><li>WHERE UPPER(customer_name) = &#39;PRADEEP&#39;: This is the filtering condition. It filters the result set to include only rows where the customer_name column, after converting to uppercase using the UPPER() function, is equal to &#39;PRADEEP&#39; in uppercase.</li></ol><ul><li>UPPER(customer_name): This function converts the customer_name column value to uppercase, ensuring a case-insensitive comparison.</li><li>&#39;PRADEEP&#39;: This is the value to which the converted customer_name is compared. It&#39;s &#39;PRADEEP&#39; in uppercase, ensuring that the comparison is also case-insensitive.</li></ul><p>So, the overall result of this query will be a list of all transactions (rows from the orders table) where the customer&#39;s name, regardless of its original case, matches &#39;PRADEEP&#39; in uppercase. This query allows you to find all transactions made by customers named &quot;Pradeep&quot; while ignoring differences in capitalization.</p><h4>Q — 8 : Self Join, manager salary &gt; employee salary?</h4><pre>SELECT<br>    e1.emp_id,<br>    e1.emp_name,<br>    e2.emp_name AS manager_name,<br>    e1.salary,<br>    e2.salary AS manager_salary<br>FROM<br>    emp AS e1<br>INNER JOIN<br>    emp AS e2 ON e1.manager_id = e2.emp_id<br>WHERE e2.salary &gt; e1.salary;</pre><ol><li><strong>SELECT Clause:</strong></li></ol><ul><li>e1.emp_id, e1.emp_name, e2.emp_name AS manager_name, e1.salary, e2.salary AS manager_salary: This part of the query specifies which columns to include in the result set.</li><li>e1.emp_id and e1.emp_name represent the ID and name of the employee (the subordinate).</li><li>e2.emp_name AS manager_name represents the name of the employee&#39;s manager. The use of AS renames the column to make it more descriptive.</li><li>e1.salary is the salary of the employee.</li><li>e2.salary AS manager_salary represents the salary of the employee&#39;s manager. Again, AS is used to rename the column.</li></ul><p><strong>2. FROM Clause:</strong></p><ul><li>FROM emp AS e1: This clause specifies the source table for the query and aliases it as e1. It represents the employees (subordinates).</li></ul><p><strong>3. INNER JOIN Clause:</strong></p><ul><li>INNER JOIN emp AS e2 ON e1.manager_id = e2.emp_id: This clause performs a self-join on the emp table. It links employees (subordinates) from e1 with their managers from e2 based on the manager_id of the employee (e1.manager_id) matching the emp_id of the manager (e2.emp_id). In other words, it establishes the relationship between employees and their managers within the same table.</li></ul><p><strong>4. WHERE Clause:</strong></p><ul><li>WHERE e2.salary &gt; e1.salary;: This is the filtering condition. It limits the result set to include only rows where the salary of the manager (e2.salary) is greater than the salary of the employee (e1.salary). This condition selects employees whose managers earn more than they do.</li></ul><p>So, the overall result of this query will be a list of employees and their respective managers, but it only includes rows where the manager’s salary is higher than that of the employee. This query helps identify employees with managers who have a higher salary.</p><h4>Q — 9 : Different types of joins?</h4><p><a href="https://www.linkedin.com/embed/feed/update/urn:li:share:7045960131649404928">Pradeepchandra Reddy S C on LinkedIn: #data #sql #database #mysql | 13 comments</a></p><h4>Q — 10 : Update query to swap gender?</h4><pre>UPDATE orders <br>SET <br>    customer_gender = CASE<br>        WHEN customer_gender = &#39;Male&#39; THEN &#39;Female&#39;<br>        WHEN customer_gender = &#39;Female&#39; THEN &#39;Male&#39;<br>    END</pre><ol><li><strong>UPDATE Clause:</strong></li></ol><ul><li>UPDATE orders: This part of the query specifies the table to be updated, which is the orders table. It indicates that you want to make changes to the data in this table.</li></ul><p><strong>2. SET Clause:</strong></p><ul><li>SET customer_gender = CASE ... END: In this section, you define the column you want to update (customer_gender) and use a CASE statement to specify how the updates should be performed.</li></ul><p><strong>3. CASE Statement:</strong></p><ul><li>CASE WHEN customer_gender = &#39;Male&#39; THEN &#39;Female&#39; WHEN customer_gender = &#39;Female&#39; THEN &#39;Male&#39; END: This CASE statement is used to conditionally update the customer_gender column. It operates as follows:</li><li>When the current value of customer_gender is &#39;Male,&#39; it replaces it with &#39;Female.&#39;</li><li>When the current value of customer_gender is &#39;Female,&#39; it replaces it with &#39;Male.&#39;</li><li>For any other values in the customer_gender column, there are no updates.</li></ul><p>So, after running this UPDATE query, the values in the customer_gender column of the orders table will be swapped. All instances of &#39;Male&#39; will be changed to &#39;Female,&#39; and all instances of &#39;Female&#39; will be changed to &#39;Male,&#39; effectively swapping the genders for those records while leaving other values in the column unaffected.</p><p>Video Link for Understanding Better —</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FIv9qBz-cyVA&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DIv9qBz-cyVA&amp;image=http%3A%2F%2Fi.ytimg.com%2Fvi%2FIv9qBz-cyVA%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/51d57bef8271a80171c66c5b942109c2/href">https://medium.com/media/51d57bef8271a80171c66c5b942109c2/href</a></iframe><p><em>I hope this was somehow useful to you, if it is say a thanks on my </em><a href="https://www.linkedin.com/in/pradeepchandra-reddy-s-c/"><em>Linkedin</em></a></p><p><strong><em>Thanks for reading! If you like the article make sure to:</em></strong></p><ul><li><strong>👏 Clap for the story (50 claps) and follow me 👉</strong></li><li><strong>📰 View more content on my medium profile</strong></li></ul><p><strong><em>🔔 Follow Me On:</em></strong></p><p><a href="https://www.linkedin.com/in/pradeepchandra-reddy-s-c/">LinkedIn</a> | <a href="https://github.com/soopertramp">GitHub</a> | <a href="https://www.instagram.com/soopertramp07/">Instagram</a> | <a href="http://topmate.io/soopertramp">Topmate</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f15a967c8656" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Solving SQL With Pradeep Day1]]></title>
            <link>https://medium.com/@pradeepchandrareddy95/solving-sql-with-pradeep-day1-aed6878994f6?source=rss-a668ce417d44------2</link>
            <guid isPermaLink="false">https://medium.com/p/aed6878994f6</guid>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[data]]></category>
            <category><![CDATA[analytics]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[problem-solving]]></category>
            <dc:creator><![CDATA[Pradeepchandra Reddy S C]]></dc:creator>
            <pubDate>Thu, 31 Aug 2023 05:15:54 GMT</pubDate>
            <atom:updated>2023-09-05T08:24:46.413Z</atom:updated>
            <content:encoded><![CDATA[<h3>Solving SQL With Pradeep Day 1</h3><figure><img alt="Solving SQL With Pradeep Day1" src="https://cdn-images-1.medium.com/max/1024/1*tJqNMvjXt_k9FD6Y54YMxA.png" /></figure><h4>Q 1 — What is the difference between WHERE and HAVING Clause in SQL ?</h4><p>The WHERE and HAVING clauses are used to filter and restrict the rows returned by a SQL query based on certain conditions.</p><p><strong>WHERE Clause:</strong></p><p>The WHERE clause is used to filter rows from a table based on a specified condition. Suppose you have a table named Orders with columns OrderID, CustomerID, and OrderAmount. You want to retrieve all orders where the order amount is greater than $100.</p><pre>SELECT *<br>FROM Orders<br>WHERE OrderAmount &gt; 100</pre><p>In this example, the WHERE clause filters the rows from the Orders table and only returns rows where the OrderAmount is greater than $100.</p><p><strong>HAVING Clause:</strong></p><p>The HAVING clause is similar to the WHERE clause but is used specifically with the GROUP BY clause. It filters rows after they have been grouped and aggregated.</p><p>Suppose you have a table named Sales with columns ProductID, Category, and SalesAmount. You want to find the total sales amount for each category and only retrieve categories with a total sales amount greater than $1000.</p><pre>SELECT Category, SUM(SalesAmount) AS TotalSales<br>FROM Sales<br>GROUP BY Category<br>HAVING SUM(SalesAmount) &gt; 1000;</pre><p>In this example, the GROUP BY clause groups the rows from the Sales table by Category, and then the HAVING clause filters the grouped results to only include categories with a total sales amount greater than $1000.</p><p>To summarize — WHERE clause is applied to Individual Rows whereas HAVING clause is applied to the aggregated values, if you have both then WHERE is applied first and then HAVING is applied <strong>(Refer Order of Execution -FROM<em>➡ </em>WHERE<em>➡ </em>GROUP BY<em>➡</em>HAVING<em>➡</em>SELECT<em>➡</em>ORDER BY<em>➡ </em>LIMIT).</strong></p><p>Video Link for Understanding Better —</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2F4xPxGX4mfb4%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D4xPxGX4mfb4&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2F4xPxGX4mfb4%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/0cf9d11680da56260b922ebd7094c9d5/href">https://medium.com/media/0cf9d11680da56260b922ebd7094c9d5/href</a></iframe><h4>Q 2 — Convert Rows to Columns and Columns to Rows without using Pivot Functions in SQL?</h4><p>Let’s say you have a table named Sales with columns ProductID, Year, and SalesAmount. You want to convert the years into columns and show the total sales amount for each year for different products.</p><p><strong>Original </strong><strong>Sales table:</strong></p><pre>| ProductID | Year | SalesAmount |<br>|-----------|------|-------------|<br>| 1         | 2021 | 1000        |<br>| 1         | 2022 | 1500        |<br>| 2         | 2021 | 800         |<br>| 2         | 2022 | 1200        |</pre><p><strong>Desired output:</strong></p><pre>| ProductID | 2021 | 2022 |<br>|-----------|------|------|<br>| 1         | 1000 | 1500 |<br>| 2         | 800  | 1200 |</pre><p>You can achieve this by using conditional aggregation and the CASE statement:</p><pre>SELECT<br>    ProductID,<br>    SUM(CASE WHEN Year = 2021 THEN SalesAmount ELSE 0 END) AS &quot;2021&quot;,<br>    SUM(CASE WHEN Year = 2022 THEN SalesAmount ELSE 0 END) AS &quot;2022&quot;<br>FROM Sales<br>GROUP BY ProductID;</pre><p>✅ We use the CASE statement to conditionally aggregate the SalesAmount based on the year.</p><p>✅ The SUM function is used to calculate the total sales amount for each year.</p><p>✅ The GROUP BY clause groups the results by ProductID.</p><p>This way, you’re effectively converting rows (years) into columns and aggregating the sales amounts accordingly.</p><p>Video Link for Understanding Better —</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FO6mDdUIvx9k%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DO6mDdUIvx9k&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FO6mDdUIvx9k%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/4ccd8fdc8b189ce7f9edb5087d250bd6/href">https://medium.com/media/4ccd8fdc8b189ce7f9edb5087d250bd6/href</a></iframe><p><strong><em>Thanks for reading! If you like the article make sure to:</em></strong></p><ul><li><strong>👏 Clap for the story (50 claps) and follow me 👉</strong></li><li><strong>📰 View more content on my medium profile</strong></li></ul><p><strong><em>🔔 Follow Me On:</em></strong></p><p><a href="https://www.linkedin.com/in/pradeepchandra-reddy-s-c/">LinkedIn</a></p><p><a href="https://github.com/soopertramp">GitHub</a></p><p><a href="https://www.instagram.com/soopertramp07/">Instagram</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=aed6878994f6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Ultimate Guide to Excel Interviews for Data Analyst Positions]]></title>
            <link>https://medium.com/@pradeepchandrareddy95/the-ultimate-guide-to-excel-interviews-for-data-analyst-positions-858cbed5301d?source=rss-a668ce417d44------2</link>
            <guid isPermaLink="false">https://medium.com/p/858cbed5301d</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[data]]></category>
            <category><![CDATA[analysis]]></category>
            <category><![CDATA[analytics]]></category>
            <category><![CDATA[excel]]></category>
            <dc:creator><![CDATA[Pradeepchandra Reddy S C]]></dc:creator>
            <pubDate>Wed, 15 Mar 2023 11:38:43 GMT</pubDate>
            <atom:updated>2023-03-15T11:38:43.795Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1b-gjD8VPrBp4myeFDgH4w.jpeg" /><figcaption>IMG — Techspot</figcaption></figure><blockquote><strong>1. What are some of the most common uses for Excel?</strong></blockquote><ol><li><strong>Data analysis and management</strong></li><li><strong>Budgeting and financial planning</strong></li><li><strong>Project management</strong></li><li><strong>Statistical analysis</strong></li><li><strong>Reporting</strong></li><li><strong>Data visualization</strong></li></ol><blockquote><strong>2. What is a spreadsheet and what is it used for?</strong></blockquote><p><strong>A spreadsheet is an electronic document consisting of rows and columns, similar to a table, which is used to store, organize, and manipulate data.</strong></p><ol><li><strong>Data entry and organization</strong></li><li><strong>Calculation and analysis</strong></li><li><strong>Budgeting and forecasting</strong></li><li><strong>Project management</strong></li><li><strong>Reporting</strong></li></ol><blockquote><strong>3. What is a cell and what are its components?</strong></blockquote><p><strong>In a spreadsheet, a cell is a single box located at the intersection of a row and a column. It is the basic unit of a spreadsheet and is used to store and manipulate data.</strong></p><ol><li><strong>Cell address</strong></li><li><strong>Data</strong></li><li><strong>Formula bar</strong></li><li><strong>Formatting</strong></li><li><strong>Function</strong></li></ol><blockquote><strong>4. What is the purpose of the Excel ribbon?</strong></blockquote><p><strong>The Excel ribbon is a graphical user interface element that contains tabs, groups, and commands, which are used to perform various tasks and functions within Microsoft Excel. The purpose of the Excel ribbon is to provide an intuitive and organized way for users to access the tools and features they need to work with their data.</strong></p><blockquote><strong>5. What is the difference between a formula and a function?</strong></blockquote><p><strong>a formula is an expression that performs a calculation or other operation on data in one or more cells. A function is a predefined formula that performs a specific operation, such as summing a range of cells or finding the maximum value in a range.</strong></p><blockquote><strong>6. How do you create a named range in Excel?</strong></blockquote><p><strong>A named range is a feature that allows you to give a name to a group of cells or a range of cells in a worksheet. By assigning a name to a range of cells, you can quickly refer to that range in formulas, charts, and other parts of your workbook.</strong></p><ol><li><strong>Select the cell or range of cells that you want to name.</strong></li><li><strong>Click on the “Formulas” tab in the ribbon menu.</strong></li><li><strong>Click on the “Define Name” button in the “Defined Names” group. This will open the “New Name” dialog box.</strong></li><li><strong>In the “Name” field, type the name that you want to give to the range.</strong></li><li><strong>In the “Scope” section, select whether you want the named range to be available in the current workbook or in a specific worksheet.</strong></li><li><strong>In the “Refers to” field, verify that the correct cell or range of cells is displayed.</strong></li><li><strong>Click on the “OK” button to create the named range.</strong></li></ol><p><strong>Once you have created a named range, you can use it in formulas and functions throughout your workbook. To do so, simply enter the name of the range instead of the cell reference in the formula or function. For example, if you have named the range A1:B10 as “my_range”, you can use the formula</strong></p><pre>=SUM(my_range)</pre><p><strong>to calculate the sum of all the cells in the range.</strong></p><blockquote><strong>7. What is the difference between a chart and a graph?</strong></blockquote><p><strong>The term “chart” tends to refer to a specific type of graphical representation, such as a line chart, bar chart, pie chart, or scatter chart. Each type of chart has its own unique features and is best suited for different types of data and analysis.</strong></p><p><strong>On the other hand, the term “graph” is often used more broadly to refer to any type of visual representation of data, including charts, diagrams, and other types of graphics. A graph can be a bar graph, a line graph, a scatter plot, a bubble chart, or any other type of visual display that represents data in a meaningful way.</strong></p><blockquote><strong>8. What is the purpose of data validation in Excel?</strong></blockquote><p><strong>The purpose of data validation in Excel is to ensure that the data entered into a worksheet is accurate, consistent, and valid. Data validation is a feature in Excel that allows you to control the type of data that is entered into a cell or range of cells, and can help to prevent errors and inconsistencies in your data.</strong></p><p><strong>With data validation, you can set rules for the type of data that is allowed in a cell, such as numeric values, dates, or text. You can also set limits on the range of values that are allowed, such as a minimum and maximum value, or a list of acceptable values.</strong></p><blockquote><strong>9. How do you use the VLOOKUP function in Excel?</strong></blockquote><p><strong>The VLOOKUP function in Excel is used to look up a value in a table or range of cells, and return a corresponding value from a different column in the same row. The syntax for the VLOOKUP function is as follows:</strong></p><pre>=VLOOKUP(lookup_value, table_array, col_index_num, [range_lookup])</pre><p><strong>lookup_value: The value you want to look up in the first column of the table.</strong></p><p><strong>table_array: The range of cells that contains the table you want to look up the value in.</strong></p><p><strong>col_index_num: The column number of the table that contains the value you want to return. This is a number starting with 1 for the leftmost column of the table.</strong></p><p><strong>range_lookup: Optional. This argument specifies whether to perform an exact match or an approximate match. Enter TRUE or omit this argument to perform an approximate match, or enter FALSE to perform an exact match.</strong></p><blockquote><strong>10. What is a filter in Excel and how do you use it?</strong></blockquote><p><strong>The FILTER function allows you to filter a range of data based on criteria you define.</strong></p><blockquote><strong>11. How do you remove duplicates in Excel?</strong></blockquote><p><strong>To remove duplicate values, click Data &gt; Data Tools &gt; Remove Duplicates.</strong></p><blockquote><strong>12. How do you use the SUBSTITUTE function in Excel?</strong></blockquote><p><strong>The SUBSTITUTE function in Excel is used to replace a specific text in a cell with another text.</strong></p><p><strong>For example, if you want to replace all occurrences of the text “apple” with “orange” in cell A1, the formula would be:</strong></p><pre>=SUBSTITUTE(A1, &quot;apple&quot;, &quot;orange&quot;)</pre><p><strong>This formula will replace all instances of the word “apple” with the word “orange” in cell A1.</strong></p><blockquote><strong>13. How do you use the NETWORKDAYS function in Excel?</strong></blockquote><p><strong>The NETWORKDAYS function in Excel is used to calculate the number of working days between two dates, excluding weekends and any holidays you specify.</strong></p><p><strong>For example, if you want to calculate the number of working days between January 1, 2023, and January 31, 2023, excluding January 1 (New Year’s Day) and January 17 (Martin Luther King Jr. Day), the formula would be:</strong></p><pre>=NETWORKDAYS(&quot;1/1/2023&quot;, &quot;1/31/2023&quot;, {&quot;1/1/2023&quot;, &quot;1/17/2023&quot;})</pre><p><strong>This formula will return the number of working days between January 1, 2023, and January 31, 2023, excluding January 1 and January 17. Note that when specifying holidays, you need to enclose the dates in quotation marks and separate them with commas within curly braces.</strong></p><blockquote><strong>14. How do you use the ROUNDUP and ROUNDDOWN functions in Excel?</strong></blockquote><p><strong>The ROUNDUP and ROUNDDOWN functions in Excel are used to round a number to a specified number of decimal places.</strong></p><p><strong>For example, if you want to round up the number 3.14159 to two decimal places, the formula would be:</strong></p><pre>=ROUNDUP(3.14159, 2)</pre><p><strong>This formula will round up 3.14159 to 3.15.</strong></p><p><strong>For example, if you want to round down the number 3.14159 to two decimal places, the formula would be:</strong></p><pre>=ROUNDDOWN(3.14159, 2)</pre><p><strong>This formula will round down 3.14159 to 3.14.</strong></p><blockquote><strong>15. What is the purpose of the MEDIAN function in Excel?</strong></blockquote><p><strong>The MEDIAN function in Excel is used to find the middle value of a range of numbers. It is often used to determine the central tendency or average of a set of data.</strong></p><p><strong>For example, if you have a range of numbers in cells A1 through A10 and you want to find the median value of that range, the formula would be:</strong></p><pre>=MEDIAN(A1:A10)</pre><p><strong>This formula will return the middle value of the range of numbers in cells A1 through A10. If there are an even number of values in the range, the median is calculated as the average of the two middle values.</strong></p><blockquote><strong>16. How do you use the STDEV and STDEVP functions in Excel?</strong></blockquote><p><strong>The STDEV and STDEVP functions in Excel are used to calculate the standard deviation of a set of data. The STDEV function calculates the standard deviation of a sample of data, while the STDEVP function calculates the standard deviation of an entire population of data.</strong></p><p><strong>For example, if you have a range of numbers in cells A1 through A10 and you want to find the standard deviation of that range as a sample, the formula would be:</strong></p><pre>=STDEV(A1:A10)</pre><p><strong>This formula will return the standard deviation of the range of numbers in cells A1 through A10.</strong></p><p><strong>For example, if you have a range of numbers in cells A1 through A10 and you want to find the standard deviation of that range as a population, the formula would be:</strong></p><pre>=STDEVP(A1:A10)</pre><p><strong>This formula will return the standard deviation of the range of numbers in cells A1 through A10 as a population.</strong></p><p><strong>In summary, if you are working with a sample of data, you should use the STDEV function, and if you are working with an entire population of data, you should use the STDEVP function.</strong></p><blockquote><strong>17. How do you use the FIND function in Excel?</strong></blockquote><p><strong>The FIND function in Excel is used to find the position of a specific character or text string within a larger text string.</strong></p><p><strong>The FIND function will return the position of the first occurrence of the character or text string within the larger text string. If the character or text string is not found within the larger text string, the function will return an error value (#VALUE!).</strong></p><p><strong>For example, to find the position of the letter “o” in the text string “Hello World”, the formula would be:</strong></p><pre>=FIND(&quot;o&quot;, &quot;Hello World&quot;)</pre><p><strong>This formula will return the value 5, which is the position of the first occurrence of the letter “o” in the text string “Hello World”.</strong></p><blockquote><strong>18. What is the purpose of the LEN function in Excel?</strong></blockquote><p><strong>The LEN function in Excel is used to find out the number of characters in a text string, including letters, numbers, spaces, and punctuation marks.</strong></p><p><strong>The syntax of the LEN function is:</strong></p><pre>=LEN(text)</pre><p><strong>where “text” is the text string for which you want to count the number of characters.</strong></p><blockquote><strong>19. How do you use the TRIM function in Excel?</strong></blockquote><p><strong>The TRIM function in Excel is used to remove extra spaces from a text string. The syntax of the TRIM function is:</strong></p><pre>=TRIM(text)</pre><p><strong>where “text” is the text string from which you want to remove extra spaces.</strong></p><blockquote><strong>20. How would you use the LEFT, MID, and RIGHT functions in combination to extract specific text from a cell in Excel?</strong></blockquote><p><strong>TRIM function with other functions in Excel. For example, you can combine the TRIM function with the LEFT, RIGHT, or MID function to extract specific parts of a text string after removing extra spaces.</strong></p><p><strong>Here is an example of how to use the TRIM function with the LEFT function:</strong></p><p><strong>Enter the formula</strong></p><pre>=LEFT(TRIM(A1),5)</pre><p><strong>in the formula bar (assuming the text string is in cell A1).</strong></p><blockquote><strong>21. What is the purpose of the UPPER and LOWER functions in Excel?</strong></blockquote><p><strong>The UPPER and LOWER functions in Excel are used to convert text to all uppercase or lowercase letters, respectively.</strong></p><p><strong>The purpose of the UPPER function is to convert all the lowercase characters in a given text string to uppercase. For example, if you have the text “hello world” in a cell, you can use the UPPER function to convert it to “HELLO WORLD” by typing formula into another cell.</strong></p><pre>=UPPER(&quot;hello world&quot;)</pre><p><strong>Similarly, the LOWER function is used to convert all the uppercase characters in a given text string to lowercase. For example, if you have the text “HELLO WORLD” in a cell, you can use the LOWER function to convert it to “hello world” by typing formula into another cell.</strong></p><pre>=LOWER(&quot;HELLO WORLD&quot;)</pre><p><strong>Both functions can be useful for formatting text in a consistent way, especially when working with large amounts of text data. They can also be used in conjunction with other Excel functions, such as CONCATENATE or LEFT, to create more complex formulas that manipulate text.</strong></p><blockquote><strong>22. How do you use the TEXT function in Excel?</strong></blockquote><p><strong>The TEXT function in Excel is used to convert a numeric value into a specific text format. The syntax of the function is as follows:</strong></p><pre>=TEXT(value,format_text)</pre><p><strong>Where “value” is the numeric value you want to convert and “format_text” is the text format you want to apply to the value.</strong></p><p><strong>Here are some examples of how to use the TEXT function in Excel:</strong></p><p><strong>Convert a date to a specific format:</strong></p><pre>=TEXT(A1,&quot;dd/mm/yyyy&quot;)</pre><p><strong>This formula will convert the date in cell A1 to the format “dd/mm/yyyy”.</strong></p><p><strong>Convert a number to a specific format:</strong></p><pre>=TEXT(A1,&quot;0.00%&quot;)</pre><p><strong>This formula will convert the number in cell A1 to a percentage with two decimal places.</strong></p><p><strong>Display leading zeros:</strong></p><pre>=TEXT(A1,&quot;00000&quot;)</pre><p><strong>This formula will display the number in cell A1 with leading zeros so that it is a 5-digit number.</strong></p><blockquote><strong>23. How do you use the DATEDIF function in Excel?</strong></blockquote><p><strong>The DATEDIF function in Excel is used to calculate the difference between two dates in years, months, or days. The syntax for the function is as follows:</strong></p><pre>=DATEDIF(start_date, end_date, unit)</pre><p><strong>Example 1: Calculating the number of years between two dates</strong></p><pre>=DATEDIF(A2, B2, &quot;Y&quot;)</pre><p><strong>In this example, the function calculates the number of years between the dates in cells A2 and B2.</strong></p><p><strong>Example 2: Calculating the number of months between two dates</strong></p><pre>=DATEDIF(A2, B2, &quot;M&quot;)</pre><p><strong>In this example, the function calculates the number of months between the dates in cells A2 and B2.</strong></p><blockquote><strong>24. How do you use the COUNTIFS function in Excel?</strong></blockquote><p><strong>The COUNTIFS function in Excel is used to count the number of cells in a range that meet multiple criteria. The syntax for the function is as follows:</strong></p><pre>=COUNTIFS(range1, criteria1, range2, criteria2, …)</pre><p><strong>You can specify up to 127 ranges and criteria in the function.</strong></p><p><strong>Example 1: Counting cells in a range that meet one criterion</strong></p><pre>=COUNTIFS(A1:A10, &quot;&gt;10&quot;)</pre><p><strong>In this example, the function counts the number of cells in the range A1:A10 that are greater than 10.</strong></p><p><strong>Example 2: Counting cells in a range that meet multiple criteria</strong></p><pre>=COUNTIFS(A1:A10, &quot;&gt;10&quot;, B1:B10, &quot;&lt;20&quot;)</pre><p><strong>In this example, the function counts the number of cells in the range A1:A10 that are greater than 10 and the corresponding cells in B1:B10 that are less than 20.</strong></p><p><strong>Example 3: Counting cells in a range that match text criteria</strong></p><pre>=COUNTIFS(A1:A10, &quot;apples&quot;)</pre><p><strong>In this example, the function counts the number of cells in the range A1:A10 that contain the text “apples”.</strong></p><p><strong>Note that when using text criteria, you need to enclose the criteria in double quotes.</strong></p><blockquote><strong>25. What is the purpose of the ROW and COLUMN functions in Excel?</strong></blockquote><p><strong>The ROW and COLUMN functions in Excel are used to return the row or column number of a specific cell or range of cells. The purpose of these functions is to simplify referencing specific cells or ranges in a formula or macro.</strong></p><p><strong>ROW Function:</strong></p><p><strong>The ROW function returns the row number of a specific cell or range of cells. The syntax for the function is as follows:</strong></p><pre>=ROW([reference])</pre><p><strong>COLUMN Function:</strong></p><p><strong>The COLUMN function returns the column number of a specific cell or range of cells. The syntax for the function is as follows:</strong></p><pre>=COLUMN([reference])</pre><blockquote><strong>26. How do you use the TRANSPOSE function in Excel?</strong></blockquote><p><strong>The TRANSPOSE function in Excel allows you to switch the orientation of a range of cells from horizontal to vertical or vice versa.</strong></p><p><strong>you can use the TRANSPOSE function directly:</strong></p><p><strong>In a blank cell, type</strong></p><pre>=TRANSPOSE(&quot; select the range of cells that you want to transpose)</pre><p><strong>The transposed range will be displayed in the cells below or beside the cell where you entered the formula. Note that the TRANSPOSE function must be entered as an array formula, which means that you need to press Ctrl+Shift+Enter instead of just Enter to confirm the formula.</strong></p><blockquote><strong>27. What is the purpose of the YEAR, MONTH, and DAY functions in Excel?</strong></blockquote><p><strong>The YEAR, MONTH, and DAY functions in Excel are used to extract the year, month, and day values from a date.</strong></p><p><strong>YEAR Function: The YEAR function returns the year value from a given date. For example, if cell A1 contains the date “01/01/2022”, the formula formula would return the value “2022”.</strong></p><pre>=YEAR(A1)</pre><p><strong>MONTH Function: The MONTH function returns the month value from a given date. For example, if cell A1 contains the date “01/01/2022”, the formula formula would return the value “1”.</strong></p><pre>=MONTH(A1)</pre><p><strong>DAY Function: The DAY function returns the day value from a given date. For example, if cell A1 contains the date “01/01/2022”, the formula formula would return the value “1”.</strong></p><pre>=DAY(A1)<br><br>=YEAR(B1)-YEAR(A1) - in cell C1. This will give you the person&#39;s age as of today.</pre><blockquote><strong>28. How would you use the CHOOSE function in Excel?</strong></blockquote><p><strong>The CHOOSE function in Excel is used to return a value from a list of values based on a specified index number.</strong></p><p><strong>For example, let’s say you have a list of fruits in cells A1 through A4, and you want to return the fruit at a specified index number. You could use the CHOOSE function as follows:</strong></p><pre>=CHOOSE(2,A1,A2,A3,A4)</pre><p><strong>In this example, the index number is 2, so the function will return the value in the second position of the list, which is the value in cell A2.</strong></p><blockquote><strong>29. How would you use the INDEX and MATCH functions in Excel?</strong></blockquote><p><strong>The INDEX and MATCH functions in Excel are often used together to look up a value in a table or range and return a corresponding value from another column in the same row.</strong></p><p><strong>Here’s an example of how to use the INDEX and MATCH functions in Excel:</strong></p><p><strong>Assuming you have a range of data in cells A1 to C4:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/553/0*HSdtXOwhhlmMeiZ3" /></figure><p><strong>If you want to retrieve the age of a person based on their name, you can use the following formula:</strong></p><pre>=INDEX(B2:B4,MATCH(&quot;Jane&quot;,A2:A4,0))</pre><p><strong>This formula will return the age of Jane, which is 30. Here’s how it works:</strong></p><p><strong>The MATCH function searches for the value “Jane” in the range A2:A4 and returns the position of the value, which is 2 (since Jane is in the second row).</strong></p><p><strong>The INDEX function retrieves the value in the range B2:B4 at the position returned by the MATCH function, which is the age of Jane (30).</strong></p><blockquote><strong>30. What is a macro in Excel and how do you create one?</strong></blockquote><p><strong>A macro in Excel is a set of instructions or code that automates repetitive tasks in a spreadsheet. Macros can be created using the Visual Basic for Applications (VBA) programming language, which is built into Excel. By creating macros, you can save time and reduce the chance of errors by automating tasks such as data entry, formatting, and calculations.</strong></p><blockquote><strong>31. How do you add comments to cells in Excel?</strong></blockquote><p><strong>You can add comments to cells in Excel by keyboard shortcut “Shift + F2” to insert a comment in the active cell.</strong></p><blockquote><strong>32. How do you freeze panes in Excel?</strong></blockquote><p><strong>Freezing panes in Excel is a useful feature that allows you to keep certain rows or columns visible while scrolling through a large worksheet.</strong></p><p><strong>Go to the “View” tab in the Excel ribbon.</strong></p><p><strong>Click on the “Freeze Panes” button, which is located in the “Window” group.</strong></p><p><strong>From the dropdown menu, select either “Freeze Panes” to freeze the rows and columns you selected in step 1, or “Freeze Top Row” or “Freeze First Column” to freeze just the top row or first column, respectively.</strong></p><blockquote><strong>33. How would you use the SUMIF function in Excel?</strong></blockquote><p><strong>The SUMIF function in Excel is used to sum values in a range that meet certain criteria.</strong></p><pre>=SUMIF(range, criteria, [sum_range])</pre><p><strong>Here’s an example of how to use the SUMIF function in Excel:</strong></p><p><strong>Suppose you have a table of sales data, with columns for “Region”, “Salesperson”, and “Sales Amount”. You want to calculate the total sales for the East region.</strong></p><p><strong>Here’s how you could do it:</strong></p><p><strong>Select a cell where you want to display the result.</strong></p><p><strong>Enter the SUMIF function:</strong></p><pre>=SUMIF(A2:A10, &quot;East&quot;, C2:C10)</pre><p><strong>This will add up the sales amounts in column C for any row where the Region column (column A) contains the text “East”.</strong></p><h3><strong>34. What is the purpose of the IF function in Excel?</strong></h3><p><strong>The IF function in Excel is used to perform a logical test and return one value if the test is true and another value if the test is false.</strong></p><pre>=IF(logicaltest, valueiftrue, value if false)</pre><p><strong>Here’s an example of how to use the IF function in Excel:</strong></p><p><strong>Suppose you have a table of test scores, with columns for “Student Name” and “Score”. You want to add a column that displays “Pass” if the score is greater than or equal to 60 and “Fail” if the score is less than 60. Here’s how you could do it:</strong></p><p><strong>Insert a new column next to the “Score” column.</strong></p><p><strong>In the first cell of the new column, enter the following formula:</strong></p><pre>=IF(B2&gt;=60, &quot;Pass&quot;, &quot;Fail&quot;)</pre><p><strong>This will test whether the score in cell B2 is greater than or equal to 60. If it is, the formula will display “Pass”; otherwise, it will display “Fail”.</strong></p><p><strong>Copy the formula down to the rest of the cells in the column. The “Pass” or “Fail” result will be displayed for each score in the table.</strong></p><blockquote><strong>35. How do you use the CONCATENATE function in Excel?</strong></blockquote><p><strong>The CONCATENATE function in Excel is used to combine two or more strings of text into a single cell.</strong></p><pre>=CONCATENATE(text1, text2, text3, …)</pre><p><strong>Example:</strong></p><p><strong>Suppose you want to concatenate the first name, last name, and email address of a list of people in three separate columns. You can use the following formula:</strong></p><pre>=CONCATENATE(A2, &quot; &quot;, B2, &quot; - &quot;, C2)</pre><p><strong>Here, A2, B2, and C2 represent the first name, last name, and email address of the first person in the list, respectively. The formula joins these text strings with a space and a hyphen to create a new text string in the selected cell.</strong></p><p><strong>Note: An alternative to using the CONCATENATE function is to use the “&amp;” operator to join text strings. For example, the above formula can be rewritten as:</strong></p><pre>=(A2 &amp; &quot; &quot; &amp; B2 &amp; &quot; - &quot; &amp; C2)</pre><p><strong>Both formulas will give you the same result.</strong></p><blockquote><strong>36. How do you use the MAX function in Excel?</strong></blockquote><p><strong>The MAX function in Excel is used to find the largest value in a range of cells.</strong></p><pre>=MAX(range)</pre><blockquote><strong>37. What is a circular reference in Excel?</strong></blockquote><p><strong>A circular reference in Excel is a situation where a formula in a cell refers to the same cell or a group of cells that contain the formula itself. This creates an endless loop, as Excel can’t determine the result of the formula since it depends on the result of the formula it refers to.</strong></p><p><strong>For example, suppose you have a cell A1 that contains the formula “=B1+1”, and cell B1 contains the formula “=A1+1”. In this case, Excel can’t calculate the result of either formula, as each formula depends on the result of the other. This creates a circular reference.</strong></p><blockquote><strong>38. How do you use the COUNT function in Excel?</strong></blockquote><p><strong>The COUNT function in Excel is used to count the number of cells in a range that contains numbers, dates, or other types of data.</strong></p><p><strong>For example, if you want to count the number of cells in the range A1:A10 that contain numbers, you would type</strong></p><pre>=COUNT(A1:A10)</pre><p><strong>in a cell and press Enter. Excel will display the result in the cell.</strong></p><p><strong>You can also use the COUNT function with multiple ranges by separating them with a comma. For example,</strong></p><pre>=COUNT(A1:A10, B1:B10)</pre><p><strong>will count the number of cells in both ranges A1:A10 and B1:B10 that contain data.</strong></p><p><strong>Note that the COUNT function only counts cells that contain numeric values, dates, or times. If you want to count cells that contain text or other non-numeric values, you should use the COUNTA function instead.</strong></p><blockquote><strong>39. What is a data table in Excel?</strong></blockquote><p><strong>A data table in Excel is a range of cells that shows the results of one or more formulas based on different input values. It allows you to perform what-if analysis by changing the input values and seeing how they affect the results.</strong></p><blockquote><strong>40. What is the purpose of the RAND function in Excel?</strong></blockquote><p><strong>The RAND function in Excel generates a random decimal number between 0 and 1. The purpose of the RAND function is to provide a random value that can be used for various purposes, such as generating test data or selecting a random item from a list.</strong></p><p><strong>Here are some examples of how the RAND function can be used in Excel:</strong></p><p><strong>Random data generation: You can use the RAND function to generate random numbers or text strings in a range of cells. For example, you might use the formula</strong></p><pre>=RAND()*100</pre><p><strong>to generate a random number between 0 and 100.</strong></p><p><strong>Random selection: You can use the RAND function in combination with other functions, such as INDEX and MATCH, to randomly select an item from a list. For example, the formula</strong></p><pre>=INDEX(A1:A10, ROUNDUP(RAND()*10,0))</pre><p><strong>would randomly select a value from the range A1:A10.</strong></p><p><strong>Simulation and modeling: You can use the RAND function to create a simulation or model by generating random input values for a formula or function.</strong></p><blockquote><strong>41. What is the purpose of the HLOOKUP function in Excel?</strong></blockquote><p><strong>The purpose of the HLOOKUP function in Excel is to search for a value in the first row of a table or range, and then return a corresponding value from a specified row in the same table or range. The “H” in HLOOKUP stands for “horizontal,” as the function searches horizontally across the first row of the table.</strong></p><p><strong>The syntax of the HLOOKUP function is as follows:</strong></p><pre>=HLOOKUP(lookupvalue, tablearray, rowindexnum, [rangelookup])</pre><p><strong>Here is an example of how the HLOOKUP function can be used in Excel:</strong></p><p><strong>Suppose you have a table that shows the scores of different students in various subjects, as shown below:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/317/0*AMxhsjWww7GFrEqU" /></figure><p><strong>To find the score of Bob in Science, you can use the HLOOKUP function as follows:</strong></p><pre>=HLOOKUP(&quot;Science&quot;, A1:D4, 2, FALSE)</pre><p><strong>This formula searches for the value “Science” in the first row of the range A1:D4 and returns the corresponding value from the second row (which represents Bob’s row) of the same column. The result is 88, which is Bob’s score in Science.</strong></p><blockquote><strong>42. How do you use the MATCH function in Excel?</strong></blockquote><p><strong>The MATCH function in Excel is used to search for a specific value in a range of cells and return its position within that range.</strong></p><p><strong>Suppose you have a list of fruits and their corresponding prices in cells A2:B6. You want to find the position of the price of “Banana” in the list using the MATCH function.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/558/0*c2wOOOB637mlVyuu" /></figure><p><strong>To do this, you would enter the following formula into a cell:</strong></p><pre>=MATCH(&quot;Banana&quot;, A2:A6, 0)</pre><p><strong>In this formula:</strong></p><p><strong>“Banana” is the lookup value you want to find the position of.</strong></p><p><strong>A2:A6 is the lookup array, which is the range of cells that contains the fruit names.</strong></p><p><strong>0 is the match type, which specifies that you want an exact match.</strong></p><p><strong>After you press Enter, Excel will return the result of “2”, since “Banana” is the second item in the lookup array. This means that the price of Banana is located in the second row of the Price column (cell B3).</strong></p><blockquote><strong>43. How do you use the INDEX function in Excel?</strong></blockquote><p><strong>The INDEX function in Excel is used to return a value from a specified position within a range or array of cells.</strong></p><pre>=INDEX(array, rownum, [columnnum])</pre><p><strong>Here’s an example of how to use the INDEX function in Excel:</strong></p><p><strong>Assuming you have a range of data in cells A1 to C4:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/555/0*h6elPbLe8C4oLr3x" /></figure><pre>=INDEX(A1:C4,3,2)</pre><p><strong>This formula returns the value “30” because it specifies row number 3 and column number 2 in the range A1 to C4.</strong></p><blockquote><strong>44. What is a named range in Excel and how do you create one?</strong></blockquote><p><strong>In Excel, a named range is a descriptive name given to a specific cell or range of cells that can be used as a shorthand reference in formulas or functions. By using a named range, you can make your formulas easier to read and understand, and also make it easier to update your formulas if the underlying data changes.</strong></p><blockquote><strong>45. How do you use the ROUND function in Excel?</strong></blockquote><p><strong>The ROUND function in Excel is used to round a number to a specified number of decimal places.</strong></p><pre>=ROUND(number, num_digits)</pre><blockquote><strong>46. How do you use the AVERAGEIF function in Excel?</strong></blockquote><p><strong>The AVERAGEIF function in Excel is used to calculate the average of a range of cells that meet a certain criteria.</strong></p><pre>=AVERAGEIF(range, criteria, [average_range])</pre><p><strong>Here are some examples of how to use the AVERAGEIF function in Excel:</strong></p><p><strong>To calculate the average of a range of cells that meet a specific criteria, use the formula:</strong></p><pre>=AVERAGEIF(A1:A10,&quot;&gt;=50&quot;)</pre><p><strong>This formula will calculate the average of the cells in the range A1:A10 that are greater than or equal to 50.</strong></p><p><strong>To calculate the average of a range of cells that meet multiple criteria, use the formula:</strong></p><pre>=AVERAGEIF(A1:A10,&quot;&gt;=50&quot;,B1:B10)</pre><p><strong>This formula will calculate the average of the cells in the range B1:B10 that are associated with cells in the range A1:A10 that are greater than or equal to 50.</strong></p><blockquote><strong>47. What is a scatter plot in Excel?</strong></blockquote><p><strong>A scatter plot, also known as a scatter chart or scatter graph, is a type of chart in Excel that is used to display the relationship between two variables. In a scatter plot, each data point is represented as a point on a two-dimensional graph, where one variable is plotted on the horizontal axis (x-axis) and the other variable is plotted on the vertical axis (y-axis).</strong></p><p><strong>Scatter plots are useful for visualizing the correlation or relationship between two variables. The scatter plot can help you determine whether the variables are positively or negatively correlated, or whether there is no correlation between the variables at all.</strong></p><blockquote><strong>48. What is a data validation list in Excel and how do you create one?</strong></blockquote><p><strong>A data validation list in Excel is a feature that allows you to create a drop-down list of items in a cell, which users can select from when entering data. This can be used to ensure that the data entered into a cell meets certain criteria, such as selecting an item from a list of predefined options.</strong></p><blockquote><strong>49. What is the purpose of the CHOOSE function in Excel?</strong></blockquote><p><strong>The purpose of the CHOOSE function in Excel is to return a value from a list of values based on a specified index number.</strong></p><p><strong>Here’s an example of how to use the CHOOSE function in Excel:</strong></p><p><strong>Suppose you have a list of sales figures for each month of the year, and you want to return the sales figure for a specific month based on its position in the list. You could use the CHOOSE function to do this by entering the following formula into a cell:</strong></p><pre>=CHOOSE(4, B2:B13)</pre><p><strong>In this example, the index number is 4, which corresponds to the fourth month of the year (April). The second argument is the range of cells containing the sales figures for each month. The CHOOSE function will return the sales figure for the fourth month of the year.</strong></p><blockquote><strong>50. How do you use the MID function in Excel?</strong></blockquote><p><strong>The MID function in Excel is used to extract a specific number of characters from a text string, starting at a specified position.</strong></p><p><strong>The syntax of the MID function is as follows:</strong></p><pre>=MID(text, startnum, numchars)</pre><p><strong>Here’s an example of how to use the MID function in Excel:</strong></p><p><strong>Suppose you have a list of names in column A, and you want to extract the first three letters of each name into column B. You could use the following formula in cell B2:</strong></p><pre>=MID(A2, 1, 3)</pre><p><strong>This formula tells Excel to extract three characters from the text string in cell A2, starting at the first character. When you copy this formula down to the other cells in column B, it will extract the first three letters from each name in the list.</strong></p><p><strong>Note that the MID function is case-sensitive</strong></p><blockquote><strong>51. How do you use the RANK function in Excel?</strong></blockquote><p><strong>The RANK function in Excel is used to determine the rank of a value in a range of values. The rank of a value is the position of the value relative to the other values in the range. The RANK function can be used to rank values in ascending or descending order.</strong></p><p><strong>The syntax for the RANK function is as follows:</strong></p><pre>=RANK(number,ref,[order])</pre><p><strong>Here’s an example of how to use the RANK function in Excel:</strong></p><p><strong>Suppose you have a list of values in cells A2:A10 and you want to rank the value in cell A3. To do this, you would use the following formula:</strong></p><pre>=RANK(A3,A2:A10)</pre><p><strong>This formula will return the rank of the value in cell A3 relative to the other values in the range A2:A10.</strong></p><p><strong>If you want to rank the value in descending order, you can add the “0” argument at the end of the formula like this:</strong></p><pre>=RANK(A3,A2:A10,0)</pre><p><strong>This formula will return the rank of the value in cell A3 in descending order. Note that in this case, the “0” argument indicates descending order, whereas omitting the argument or using a non-zero argument would indicate ascending order.</strong></p><blockquote><strong>52. How do you use the CONCAT function in Excel?</strong></blockquote><p><strong>The CONCAT function in Excel is used to concatenate (i.e., join together) two or more strings of text or cell values. This function is available in Excel 2016 and later versions.</strong></p><p><strong>The syntax for the CONCAT function is as follows:</strong></p><pre>=CONCAT(text1,[text2],…)</pre><p><strong>Here’s an example of how to use the CONCAT function in Excel:</strong></p><p><strong>Suppose you have two columns of names in cells A2:A6 and B2:B6, and you want to create a single column of full names by concatenating the first and last names. To do this, you can use the following formula in cell C2:</strong></p><pre>=CONCAT(A2,&quot; &quot;,B2)</pre><p><strong>This formula will concatenate the values in cells A2 and B2, separated by a space, to create a single text string with the full name. You can then copy this formula down to cells C3:C6 to concatenate the remaining names.</strong></p><blockquote><strong>53. How do you use the MAX and MIN functions in Excel?</strong></blockquote><p><strong>You can use the MAX and MIN functions in Excel to find the highest and lowest values in a range of cells, respectively.</strong></p><pre>=MAX(A1:A10)<br><br><br>=MIN(A1:A10)</pre><blockquote>I hope this was somehow useful to you, if it is say a thanks on my LinkedIn.</blockquote><p><strong><em>Thanks for reading! If you like the article make sure to:</em></strong></p><ul><li><strong>👏 Clap for the story (50 claps) and follow me 👉</strong></li><li><strong>📰 View more content on my medium profile</strong></li></ul><blockquote><strong><em>🔔 Follow Me: [Linkedin](</em></strong><a href="https://www.linkedin.com/in/pradeepchandra-reddy-s-c/"><em>https://www.linkedin.com/in/pradeepchandra-reddy-s-c/</em></a><em>)</em></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=858cbed5301d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to create and interact with a MySQL database from Python]]></title>
            <link>https://medium.com/@pradeepchandrareddy95/how-to-create-and-interact-with-a-mysql-database-from-python-e121ebd47d68?source=rss-a668ce417d44------2</link>
            <guid isPermaLink="false">https://medium.com/p/e121ebd47d68</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[mysql]]></category>
            <category><![CDATA[database]]></category>
            <dc:creator><![CDATA[Pradeepchandra Reddy S C]]></dc:creator>
            <pubDate>Thu, 09 Feb 2023 13:04:41 GMT</pubDate>
            <atom:updated>2023-02-09T13:04:41.010Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zRXyCPD5L7Ele2NkoNdDYg.jpeg" /><figcaption>Pic Credit — Turing</figcaption></figure><p><strong>MySQL</strong> is one of the widely used Relational Database Management Systems (RDBMS) for managing data. With Python, we can interact with MySQL databases and perform various operations such as creating tables, inserting data, updating records, and much more.</p><p><strong>In this article, we will explore the steps to create and interact with a MySQL database from Python using the MySQL Connector library.</strong></p><p><strong>Step 1 — Install the MySQL Connector library: </strong>You can install the MySQL Connector library using the pip command.</p><pre>pip install mysql-connector-python</pre><p><strong>Step 2 — Establish a connection to the database:</strong> You can establish a connection to a MySQL database using the connect( ) method of the mysql.connector module.</p><pre>import mysql.connector<br><br>mydb = mysql.connector.connect(<br>  host=&quot;hostname&quot;,<br>  user=&quot;username&quot;,<br>  password=&quot;password&quot;,<br>  database=&quot;database_name&quot;<br>)</pre><p><strong>Step 3 — Create a cursor object:</strong> A cursor is a temporary storage area in a database management system. You can use it to execute SQL statements and retrieve the results.</p><pre>mycursor = mydb.cursor()</pre><p><strong>Step 4 — Create a database: </strong>To create a database, you can execute the SQL statement (Check Video) using the execute( ) method of the cursor object.</p><pre>mycursor.execute(&quot;CREATE DATABASE database_name&quot;)</pre><p><strong>Step 5 — Create a table: </strong>To create a table in the database, you can execute the SQL statement (Check Video) using the execute( ) method of the cursor object.</p><pre>mycursor.execute(&quot;CREATE TABLE table_name (column1 datatype, column2 datatype, column3 datatype)&quot;)</pre><p><strong>Step 6 — Insert data into a table:</strong> To insert data into a table, you can execute the (Check Video) SQL statement using the execute( ) method of the cursor object.</p><pre>sql = &quot;INSERT INTO table_name (column1, column2, column3) VALUES (%s, %s, %s)&quot;<br>val = (&quot;value1&quot;, &quot;value2&quot;, &quot;value3&quot;)<br>mycursor.execute(sql, val)</pre><p><strong>Step 7 — Commit the changes:</strong> After executing the SQL statements, you need to commit the changes to the database using the commit( ) method of the database connection object.</p><pre>mydb.commit()</pre><p><strong>Step 8 — Retrieve data from a table:</strong> To retrieve data from a table, you can execute a SELECT statement using the execute( ) method of the cursor object and fetch the results using the fetchall( ) method.</p><pre>mycursor.execute(&quot;SELECT * FROM table_name&quot;)<br>myresult = mycursor.fetchall()<br><br>for x in myresult:<br>  print(x)</pre><p><strong>Step 9 — Close the connection:</strong> Finally, you need to close the connection to the database using the close() method of the database connection object.</p><pre>mydb.close()</pre><p>This is a basic overview of how to create and interact with a MySQL database from Python. You can refer to the official documentation of the MySQL Connector library for more information and advanced usage: <a href="https://dev.mysql.com/doc/connector-python/en/">https://dev.mysql.com/doc/connector-python/en/</a></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FVwblNMc09J0%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DVwblNMc09J0&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FVwblNMc09J0%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/33da46c90f40a455db0b016b5c251a4b/href">https://medium.com/media/33da46c90f40a455db0b016b5c251a4b/href</a></iframe><p><strong><em>Thanks for reading! If you like the article make sure to:</em></strong></p><ul><li><strong>👏 Clap for the story (50 claps) and follow me 👉</strong></li><li><strong>📰 View more content on my medium profile</strong></li></ul><blockquote><strong><em>🔔 Follow Me: [Linkedin](</em></strong><a href="https://www.linkedin.com/in/pradeepchandra-reddy-s-c/"><em>https://www.linkedin.com/in/pradeepchandra-reddy-s-c/</em></a><em>)</em></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e121ebd47d68" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[         …]]></title>
            <link>https://medium.com/@pradeepchandrareddy95/-7312eda632f7?source=rss-a668ce417d44------2</link>
            <guid isPermaLink="false">https://medium.com/p/7312eda632f7</guid>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[non-technical]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[careers]]></category>
            <dc:creator><![CDATA[Pradeepchandra Reddy S C]]></dc:creator>
            <pubDate>Sat, 04 Feb 2023 04:21:08 GMT</pubDate>
            <atom:updated>2023-02-04T04:31:58.703Z</atom:updated>
            <content:encoded><![CDATA[<h3>𝗛𝗼𝘄 𝘁𝗼 𝗕𝗲𝗴𝗶𝗻 𝗮 𝗖𝗮𝗿𝗲𝗲𝗿 𝗶𝗻 𝗗𝗮𝘁𝗮 𝗦𝗰𝗶𝗲𝗻𝗰𝗲 𝘄𝗶𝘁𝗵 𝗡𝗼 𝗧𝗲𝗰𝗵𝗻𝗶𝗰𝗮𝗹 𝗕𝗮𝗰𝗸𝗴𝗿𝗼𝘂𝗻𝗱</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yEyOZL7isPv4C2khejUZkQ.jpeg" /><figcaption>Photo Credits — Pexels by [Mikhail Nilov](<a href="https://www.pexels.com/photo/man-people-night-dark-6963098/">https://www.pexels.com/photo/man-people-night-dark-6963098/</a>)</figcaption></figure><p>Here are the 5 tips that I think are the most important:</p><p>𝟭. 𝗕𝘂𝗶𝗹𝗱 𝗮 𝘀𝘁𝗿𝗼𝗻𝗴 𝗳𝗼𝘂𝗻𝗱𝗮𝘁𝗶𝗼𝗻: Start by learning the basics of mathematics, statistics, and programming. There are many free online resources to help you get started.</p><p>𝟮. 𝗚𝗮𝗶𝗻 𝗽𝗿𝗮𝗰𝘁𝗶𝗰𝗮𝗹 𝗲𝘅𝗽𝗲𝗿𝗶𝗲𝗻𝗰𝗲: Participate in online data science competitions, work on personal projects, or seek internships to apply the concepts you have learned and build your portfolio.</p><p>𝟯. 𝗡𝗲𝘁𝘄𝗼𝗿𝗸 𝗮𝗻𝗱 𝘀𝘁𝗮𝘆 𝘂𝗽𝗱𝗮𝘁𝗲𝗱 (𝗠𝗼𝘀𝘁 𝗜𝗺𝗽𝗼𝗿𝘁𝗮𝗻𝘁): Attend data science events, join online communities, and connect with data scientists to stay updated with the latest trends and developments in the field. Networking can also lead to job opportunities.</p><p>𝟰. 𝗦𝗽𝗲𝗰𝗶𝗮𝗹𝗶𝘇𝗲 𝗶𝗻 𝗮 𝘀𝗽𝗲𝗰𝗶𝗳𝗶𝗰 𝗮𝗿𝗲𝗮: Choose a specific area of data science that interests you and focus on becoming an expert in that area. This could be machine learning, natural language processing, data visualization, etc.</p><p>𝟱. 𝗚𝗲𝘁 𝗰𝗲𝗿𝘁𝗶𝗳𝗶𝗲𝗱 (𝗧𝗼𝘁𝗮𝗹𝗹𝘆 𝗢𝗽𝘁𝗶𝗼𝗻𝗮𝗹): Consider getting certified in a relevant technology or programming language. This can help demonstrate your knowledge and skills to potential employers.</p><p>Breaking into a new field can be challenging, but it’s important to stay motivated and persistent.</p><p>𝗞𝗲𝗲𝗽 𝗹𝗲𝗮𝗿𝗻𝗶𝗻𝗴, 𝗻𝗲𝘁𝘄𝗼𝗿𝗸𝗶𝗻𝗴, 𝗮𝗻𝗱 𝗴𝗮𝗶𝗻𝗶𝗻𝗴 𝗲𝘅𝗽𝗲𝗿𝗶𝗲𝗻𝗰𝗲, 𝗮𝗻𝗱 𝗱𝗼𝗻’𝘁 𝗯𝗲 𝗮𝗳𝗿𝗮𝗶𝗱 𝘁𝗼 𝘁𝗮𝗸𝗲 𝗿𝗶𝘀𝗸𝘀 𝗮𝗻𝗱 𝘁𝗿𝘆 𝗻𝗲𝘄 𝘁𝗵𝗶𝗻𝗴𝘀.</p><p><strong><em>Thanks for reading! If you like the article make sure to:</em></strong></p><ul><li><strong>👏 Clap for the story (50 claps) and follow me 👉</strong></li><li><strong>📰 View more content on my medium profile</strong></li></ul><blockquote><strong>🔔 Follow Me: [Linkedin](</strong><a href="https://www.linkedin.com/in/pradeepchandra-reddy-s-c/">https://www.linkedin.com/in/pradeepchandra-reddy-s-c/</a>)</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7312eda632f7" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>