Questions
30 of 30
1What are functions and operators in MySQL, and how are they different?
2What is the difference between single-row functions and aggregate functions?
3Give examples of commonly used string functions in MySQL.
4What is the use of the CONCAT() function? How is it different from using ||?
5Explain the difference between NOW(), CURDATE(), and SYSDATE().
6What are arithmetic operators in MySQL? Give examples.
7What is the difference between the = and <=> operators?
8What does the DISTINCT keyword do when used with aggregate functions like COUNT()?
9What is the difference between IFNULL() and COALESCE() functions?
10What are logical operators in MySQL, and how do AND, OR, and NOT work?
11What is the difference between LENGTH() and CHAR_LENGTH() functions?
12How does MySQL handle type conversion when using operators on different data types?
13What is the difference between ROUND(), TRUNCATE(), and FORMAT() functions?
14Explain how CASE and IF() functions can be used for conditional logic.
15What are comparison operators in MySQL, and how are they used with NULL values?
16How do aggregate functions like AVG(), SUM(), and MAX() behave when NULLs are present?
17Explain the use of REGEXP and LIKE operators. What’s the difference between them?
18What are user-defined functions (UDFs), and how do they differ from stored procedures?
19What are the differences between DATE_ADD() and ADDDATE() functions?
20How can you use STR_TO_DATE() and DATE_FORMAT() for converting and displaying date values?
21How does MySQL internally optimize and cache results of deterministic functions?
22What are window functions in MySQL 8.0, and how do they differ from aggregate functions?
23Explain the difference between RANK(), DENSE_RANK(), and ROW_NUMBER() window functions.
24How does MySQL evaluate operator precedence when multiple operators are used in a single expression?
25Can functions be used in the WHERE clause? What are the performance implications?
26How can you use JSON_EXTRACT() and JSON_CONTAINS() to work with JSON data in MySQL?
27What are the performance trade-offs of using scalar functions inside JOIN or GROUP BY clauses?
28Explain how collation affects comparison operators in string functions.
29What are deterministic and non-deterministic functions? How does this affect replication and indexes?
30How would you combine multiple functions and operators to clean, transform, and aggregate data efficiently in one query?
30 / 30

How would you combine multiple functions and operators to clean, transform, and aggregate data efficiently in one query?

Combining Functions and Operators for Data Cleaning, Transformation, and Aggregation in MySQL

MySQL allows combining multiple functions and operators in a single query to clean, transform, and aggregate data efficiently. This enables complex transformations without multiple query passes.

1. Data Cleaning
  1. 1

    Use string functions (TRIM, REPLACE, UPPER/LOWER) to standardize text.

  2. 2

    Use COALESCE() or IFNULL() to handle NULL values.

  3. 3

    Example: Remove extra spaces and standardize case: UPPER(TRIM(name)).

2. Data Transformation
  1. 1

    Apply arithmetic and date functions to derive new values.

  2. 2

    Example: Adjust salary with bonus: salary + IFNULL(bonus, 0).

  3. 3

    Convert string dates to proper date type: STR_TO_DATE(order_date_str, '%d-%m-%Y').

3. Aggregation
  1. 1

    Use aggregate functions like SUM(), AVG(), COUNT() combined with transformations.

  2. 2

    Example: Compute total adjusted salary per department: SUM(salary + IFNULL(bonus,0)).

  3. 3

    GROUP BY can be combined with transformed expressions to create meaningful summaries.

Example Query Combining Multiple Functions and Operators
4. Optimization Tips
  1. 1

    Prefer deterministic functions for computed columns or indexes to improve performance.

  2. 2

    Avoid wrapping indexed columns in functions in WHERE or JOIN clauses to allow index usage.

  3. 3

    Consider generated columns for frequently computed transformations.

In summary: Combining string, arithmetic, and date functions with operators in a single query allows efficient cleaning, transformation, and aggregation of data. Careful use of deterministic functions, indexing strategies, and generated columns ensures both correctness and performance.

Difficulty: 6/10
Topics: data cleaning, SQL functions, aggregation

Scenario Questions

0-2 years experience
  1. 1

    We have an orders table with columns order_id, order_date, amount, and a nullable discount. Write a single MySQL query that treats null discounts as 0, calculates net_amount = amount - discount, and returns total net_amount per month.

  2. 2

    How would you use COALESCE together with DATE_FORMAT to group sales by month while handling rows where order_date is NULL?

  3. 3

    Given a users table with first_name and last_name, write a query that concatenates them into full_name and counts distinct users in one step.

2-5 years experience
  1. 1

    Our legacy orders table stores dates as strings in mixed formats and amounts as strings with a '$' prefix. How would you write a single query that cleans the dates, strips the currency symbol, converts amounts to numbers, and returns total revenue per day?

  2. 2

    A new status column can be NULL, 'completed', or 'canceled'. You need a report that treats NULL as 'unknown' and aggregates counts per status, but your CASE expression is giving wrong totals. How would you debug and fix it?

  3. 3

    We need a leaderboard that ranks users by total purchase amount, excluding rows where is_test = 1. Show how you would combine functions and a window function in one query to compute rank and total, and mention any performance concerns.

5-8 years experience
  1. 1

    Our nightly analytics job runs a massive MySQL query that uses IFNULL, DATE_TRUNC, JSON_EXTRACT, and multiple aggregations, and it now takes over an hour. How would you redesign the query or underlying schema to cut runtime while keeping a single‑query approach?

  2. 2

    We are moving from MySQL 5.7 to 8.0 and want to replace several nested subqueries with CTEs and window functions for cleaning and aggregation. What pitfalls should we watch for, and how would you ensure the new query is both correct and performant?

  3. 3

    Design a reusable view or stored procedure that encapsulates common cleaning, transformation, and aggregation logic used by multiple reports. What considerations around maintainability, security, and query planning would you address?

8+ years experience
  1. 1

    dozens of services need cleaned and aggregated data from a shared MySQL instance. How would you architect a central data‑access layer or service that consolidates these transformations, minimizes duplication, and scales with traffic?

  2. 2

    We plan to deprecate a legacy MySQL schema that contains messy data and replace it with a clean data warehouse over several years. Describe how you would orchestrate the migration so existing queries that combine functions and operators continue to work or are safely transitioned.

  3. 3

    When exposing complex cleaning and aggregation logic as a public API, what strategies would you use to version, test, and monitor the underlying MySQL queries to avoid breaking downstream consumers?

Follow-up Questions

  • Which indexes would you add to make that query run faster?
  • How would the query change if the table grew tenfold?
  • Why did you pick COALESCE instead of IFNULL in this case?