Blog

Find the most common value (mode) in a specific column.

This question was asked in Interview at black rock. Read more

Company: BlackRock

CTC: 26LPA

SourceLinkedIn

SQL Interview Question

Q. Find the most common value (mode) in a specific column.

-- Create the Products table
CREATE TABLE Products (
    ProductID INT PRIMARY KEY,
    ProductName VARCHAR(100),
    Category VARCHAR(50),
    Price DECIMAL(10, 2)
);

-- Insert sample data into Products table
INSERT INTO Products VALUES
(1, 'Laptop', 'Electronics', 800.00),
(2, 'Laptop', 'Electronics', 300.00),
(3, 'Headphones', 'Electronics', 50.00),
(4, 'Laptop', 'Electronics', 800.00),
(5, 'Tablet', 'Electronics', 300.00),
(6, 'Tablet', 'Electronics', 300.00),
(7, 'Chair', 'Furniture', 120.00),
(8, 'Table', 'Furniture', 250.00),
(9, 'Laptop', 'Electronics', 800.00),
(10, 'Desk', 'Furniture', 200.00);

See this code on db-fiddle

Solution

SELECT 
    ProductName,
    COUNT(*) AS Frequency
FROM 
    Products
GROUP BY 
    ProductName
ORDER BY 
    Frequency DESC
LIMIT 1;

Explanation

We have selected ProductName along with the count of rows as Frequency, grouping the data by ProductName. To display the product with the highest frequency at the top of the table, we used ORDER BY Frequency DESC. Finally, the LIMIT clause ensures that only the top record is selected.


I hope this would have been helpful for you, consider sharing it with your friends. thank you.

Spread the love

Recent Posts

Mastering Pivot Table in Python: A Comprehensive Guide

Pivot tables are a powerful tool for summarizing and analyzing data, and Python’s Pandas library…

1 week ago

Data Science Interview Questions Section 3: SQL, Data Warehousing, and General Analytics Concepts

Welcome to Section 3 of our Data Science Interview Questions series! In this part, we…

2 weeks ago

Data Science Interview Questions Section 2: 25 Questions Designed To Deepen Your Understanding

Welcome back to our Data Science Interview Questions series! In the first section, we explored…

2 weeks ago

Data Science Questions Section 1: Data Visualization & BI Tools (Power BI, Tableau, etc.)

Data Science Questions in Section 1 focus on the essential concepts of Data Visualization and…

2 weeks ago

Optum Interview Questions: 30 Multiple Choice Questions (MCQs) with Answers

In this article, we’ve compiled 30 carefully selected multiple choice questions (MCQs) with answers to…

2 weeks ago

Day 15 of Learning Python for Data Science: Exploring Matplotlib Visualizations and EDA

Welcome to Day 15 of our Python for Data Science journey!On Day 15, we dived…

2 weeks ago