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

SQL Interview Question at Zomato for a Data analyst Position (0-3 Years) – | Shared By An Experienced Data Analyst

SQL Interview Question at Zomato: These questions were recently asked in interview at Zomato, you…

6 days ago

The Ultimate Guide to SQL Indexing and Query Optimization

Introduction: SQL Indexing and Query Optimization SQL indexing is a critical concept that can drastically…

2 weeks ago

SQL Interview Questions Asked In Walmart For Data Analyst Post | CTC – 18 LPA | Learn With Curious Club!!

This article is about the SQL Interview Questions asked by Walmart for their Data Analyst…

2 weeks ago

SQL Interview Questions for Deloitte Data Engineer Roles: Your Ultimate Prep Guide

You must be able to answer these SQL Interview Questions if you are applying for…

3 weeks ago

Data Analyst SQL Interview Questions | EY (Ernst & Young) | Shared By An Experienced Data Analyst

This article tackles common SQL Interview Questions asked by EY, offering detailed solutions and explanations…

3 weeks ago

1164 Product Price at a Given Date

1164. Product Price at a Given Date: Learn how to track and select price from…

1 month ago