Skip to main content

SQL Interview Question - 6


Consider a table named sales_data with the following schema:


CREATE TABLE sales_data (
    id INT PRIMARY KEY,
    amount DECIMAL(10,2),
    region VARCHAR(50)
);

Assume the table contains the following data:

id amount region
1 100.00 East
2 200.00 West
3 NULL North
4 150.00 East
5 120.00 NULL
6 180.00 West
7 NULL NULL
8 300.00 South

Write a SQL query to calculate the total sales amount for each region, including NULL regions, and display the result in descending order of the total sales amount. If a region is NULL or if there are NULL values in the amount column, treat them as 0 in the calculation.


Solution


SELECT 
    COALESCE(region, 'Unknown') AS region,
    COALESCE(SUM(COALESCE(amount, 0)), 0) AS total_sales
FROM 
    sales_data
GROUP BY 
    region
ORDER BY 
    total_sales DESC;

Explanation:

  • We use the COALESCE function to handle NULL values. If region or amount is NULL, it's replaced with 'Unknown' or 0 respectively.
  • The SUM function calculates the total sales amount for each region.
  • The GROUP BY clause groups the results by region.
  • The ORDER BY clause sorts the result set in descending order of total sales amount.

 

Share your alternate solution in comments.


Check our offerings below!


Success Stories



See all the Success Stories - here
Testimonials - here


You can check out other TakeOff Talent offerings that have helped 7,000+ people land jobs.

Offerings
📄 CV Review
📘 200 most-asked SQL interview questions with detailed solutions
📘 200 most-asked Python interview questions with detailed solutions
📊 SQL Crash Course
✍️ CV Writing for freshers
✍️ CV Writing
🛠️ Portfolio Project
🗣️ English Speaking Practice (Live 1:1)
🎯 Job Search Mentorship Package

  In case of any questions around services above, write to us at vibhanshu@takeofftalent.com

  Connect with our founder on Linkedin - https://www.linkedin.com/in/vibvibgyor/



Video Gallery



Check more videos here>>.

Other popular job openings

Boston Consulting Group (BCG) is hiring for a fresher entry level Data Analyst Associate role in India

Position: Business Intelligence & Analytics Associate (BI&A Associate aka Data Analyst) Company: Boston Consulting Group (BCG) Location: Gurgaon, Haryana, India Job type: Full-time Job mode: Onsite Job requisition id: 55878 Years of experience: 0 to 3 years Company Description Boston Consulting Group (BCG) is a global management consulting firm that partners with leaders in business and society to help them navigate complex challenges and drive transformation. Established in 1963, BCG pioneered modern business strategy and continues to evolve by integrating cutting-edge digital, human, and design capabilities into its consulting practices. The firm focuses on helping clients achieve sustainable growth, operational efficiency, and competitive advantage through strategic and analytical excellence. BCG operates across industries such as technology, healthcare, energy, financial services, and more, leveraging global expertise and local insights....

McKinsey & Company is hiring for a fresher entry level Junior Data / Insights Analyst role in India

Position: Junior Data Insights Analyst (Junior Capabilities & Insights Analyst) Company: McKinsey & Company Location: Gurugram, India Job type: Full-time Job mode: Onsite Job requisition id: 103315 Years of experience: 0–1 year Company Description McKinsey & Company is a global management consulting firm known for solving complex business challenges through data-driven insights and strategic thinking. The company partners with organizations across industries to transform their performance, strengthen operations, and deliver sustainable growth. With a strong focus on innovation and digital transformation, McKinsey helps clients harness technology to design better products, services, and experiences. Its team spans over 65 countries, comprising professionals from more than 100 nationalities, united by a mission to create meaningful impact. The firm fosters a culture of collaboration and continuous learning, where curiosity and creativity are deepl...

American Express is hiring for a fresher entry level Data Analyst role in India

Position: Analyst Data Analytics Company: American Express Location: Gurugram India Job type: Full time Job mode: Hybrid Job requisition id: 25020962 Years of experience: Not mentioned in the original description, but as per JD looks like an entry level role: 0-3 years of experience Company description American Express has built its identity through a long history that spans more than a century with a culture shaped by trust, reliability, and consistent service to customers. The company supports its people through several programs that focus on overall well being with attention to physical health, financial stability, and emotional strength. It encourages individuals to explore new ideas and share opinions since these contributions help the company grow and evolve in meaningful ways. The company invests in platforms and programs that help employees develop and move forward in their journey while feeling supported at every stage. There is a focus o...

NielsenIQ is hiring for a fresher entry level Data Operations Analyst role in India

Position: Data Operations Analyst Company: NielsenIQ also known as NIQ Location: Chennai Tamil Nadu India Job type: Full time Job mode: Onsite Job requisition id: Not mentioned in the job post Years of experience: Entry level up to three years Company description NIQ operates as a global consumer intelligence and data measurement organization. It studies how buyers behave across retail categories so that companies can make smarter commercial choices. The company specializes in gathering structured and unstructured information from businesses and store systems. This material is processed to produce insights that guide product decisions promotions and pricing strategies. NIQ was created by combining long standing measurement expertise with modern data technology. It works across more than one hundred countries and touches products used by the majority of the worlds population. The organization maintains long term partnerships with co...

Navi is hiring for a fresher entry level Data Scientist role in India

Position: Data Scientist - I Company: Navi Technologies Limited Location: Bengaluru, Karnataka, India Job Type: Full-time Job Mode: On-site Job Requisition ID: Not specified Years of Experience: 0-3 years Company Description Navi Technologies Limited is one of India’s most rapidly expanding fintech organizations, established in 2018 by Sachin Bansal and Ankit Agarwal. The company’s mission is to transform the financial landscape of India by providing accessible, simple, and technology-driven financial solutions that serve individuals from all walks of life. Navi offers a diverse range of services including Personal Loans, Home Loans, UPI payments, Health and General Insurance, Mutual Funds, and Gold products, all designed with a digital-first approach. The organization focuses on building intuitive and affordable financial tools, ensuring inclusivity for millions of Indians who are either new to credit or underserved by traditional financial systems. Innov...

Nonrel Technologies is hiring for a fresher entry level Data Scientist role in India

Position: Data Scientist Company: Nonrel Technologies Location: Bengaluru, Karnataka, India Job type: Full-time Job mode: Hybrid Job requisition id: Not specified Years of experience: 0-3 years (Entry Level) Company Description Nonrel Technologies is an IT software company focused on digital transformation through advanced software and analytics-driven solutions. The company specializes in creating scalable, secure, and customized software applications that empower organizations to improve efficiency and competitiveness. It serves diverse industries, providing services such as cloud computing, IT consulting, custom software development, and data analytics. The team is composed of talented engineers, developers, and consultants who work collaboratively with clients to design impactful technology strategies. Nonrel Technologies believes in innovation, reliability, and quality as its foundation, helping organizations embrace digital-first approaches that simp...

American Express is hiring for a fresher entry level Data Scientist role in India

Position: Analyst Data Science (Data Scientist) Company: American Express Location: Gurugram, Haryana, India Job type: Full time Job mode: Hybrid Job requisition id: 25020903 Years of experience: 0 to 3 years Company description American Express is a global company trusted by millions of customers due to its long legacy of performance in financial services, payments, travel and customer service. The company has existed for more than a century and continues to stand strong as a well known name in the financial sector. Employees who join the company enter a supportive environment where people are encouraged to grow, speak freely and implement new ideas. The organization encourages learning inside the office and through a wide range of professional coaching programs. The general culture is built on honesty, care for customers, positive attitude and collaboration across divisions. People who work here receive strong support syste...

Swiggy is hiring for a fresher entry level Data Scientist 1 role in India

Position: Data Scientist I Entry level analytical role focused on building data powered solutions Involves work across the full project cycle from idea to deployment Requires curiosity, structured thinking, and strong command of computational methods Involves supporting product and engineering groups with measurable outcomes Company: Swiggy Technology first consumer platform with a strong focus on research driven work Known for large scale delivery systems and complex operational challenges Encourages experimenting, publishing, and internal knowledge exchange Fosters a culture where data teams influence strategy through factual insights Location: Bangalore Karnataka Set within a major technology ecosystem with access to a wide pool of experts Work environment shaped by fast moving product cycles and collaboration City culture supports constant learning and exploration in engineering fields Job type: Full time role Demands consistency and accounta...

TransUnion is hiring for a fresher entry level Data Analyst role in India

Position: Data Analyst Company: TransUnion Location: Hyderabad, India Job type: Full time Job mode: Hybrid Job requisition id: 19037960 Years of experience: 1 year of experience in SQL, etc. (it can be your personal, project experience as well , not mandatory a full time job experience) Company description TransUnion operates as a global information and insights company with thousands of employees across many countries. The organization focuses on helping individuals and businesses make decisions backed by data. Its work involves providing accurate and reliable data so that transactions can happen with confidence. The company uses technology, data management approaches, and digital platforms to expand its offerings. TransUnion aims to ensure that every individual is represented in the data world in a fair manner. The company works in various domains related to credit, fraud prevention, analytics, risk assessment, and marketin...

myKaarma is hiring for a fresher entry level Data Analyst role in India

Position: Data Analyst Focus on turning complex information into clear, useful insights Supports product and business users with analytical solutions Works closely with multiple teams to build clarity around product performance and customer behavior Helps improve decision making by providing accurate data Company: myKaarma Provides software solutions for dealership service centers Uses communication, scheduling, and digital payment tools in one platform Brings ease and simplicity to interactions between dealership teams and customers Inspired by principles of efficiency used in world class production systems Encourages innovation and continuous learning Aims to improve the entire service experience in the automotive world Location: Noida, Uttar Pradesh, India Hybrid working structure with a mix of office and home based work Job type: Full time role Requires active involvement with teams Expected to support business on ongoing ...