Answer: SQL Modes
Explore how to modify SQL modes in MySQL sessions to control query behavior and data validation. Understand the effects of ANSI_QUOTES on identifier quoting and ONLY_FULL_GROUP_BY on grouping rules. This lesson helps you configure session-specific SQL modes to write strict and deterministic SQL queries for accurate reporting.
We'll cover the following...
Solution
The solution is given below:
Explanation
The explanation of the solution code is given below:
Lines 1–6: These modify the SQL mode only for the current session.
@@SESSION.sql_modereturns the current list of active SQL modes as a comma-separated string.CONCAT_WS(',', ...)builds a new list by appendingANSI_QUOTESandONLY_FULL_GROUP_BYto the existing modes, inserting commas as needed and avoiding duplicate separators.SET SESSION sql_mode = ...applies the updated mode list only to this connection, without changing server-wide defaults.
Line 9: This selects the effective SQL mode for the current session and aliases it as
reporting_session_sql_mode. This provides a quick verification step to confirm thatANSI_QUOTESandONLY_FULL_GROUP_BYare now active.Lines 11–18: These define the main reporting query.
The
SELECTlist returns each customer’s ID and name along withTotalSpent, computed using theSUMaggregate function.Double quotes around
"c","o","CustomerID","CustomerName", and ...