{"id":"code-work-js-sql-second-salary-mysql57","audience":"job-seekers","category":"coding","track":"coding","technique":"context","title":"Practice a take-home SQL question on an old MySQL","free":false,"task":"You're practicing for a take-home that runs on MySQL 5.7, which has no window functions and no WITH clauses. Write a query that returns each department's second-highest distinct salary, with columns named exactly department and second_salary; departments with only one distinct salary simply don't appear. One code block, at most 10 lines, then under 25 words outside the code.","materialLabel":"Practice table from the prep packet (attached to your prompt automatically)","material":"PREP PACKET - SQL practice set 3\n\nTable: employees\nid | name | department | salary\n1 | Ana | Data | 98000\n2 | Raj | Data | 98000\n3 | Mei | Data | 91000\n4 | Lou | Data | 85000\n5 | Pat | Ops | 64000\n6 | Jae | Ops | 71000\n7 | Theo | Design | 88000\n\nExpected result for this sample:\nData | 91000\nOps | 64000\n(Design has only one salary, so it's not listed)\n\nNotes:\n- The packet's answer key uses a newer database.\n- Interviewers say they read the query for clarity as much as correctness.\n- Practice set 4 covers joins.\n- Ties at the top (Ana and Raj) count as one salary.","rules":[{"type":"codeBlocks","count":1},{"type":"maxCodeLines","max":10},{"type":"maxProseWords","max":25}],"mistakes":[{"title":"Not naming the database version","why":"The AI reaches for DENSE_RANK, which MySQL 5.7 doesn't have.","fix":"Add: \"MySQL 5.7: no window functions, no WITH.\"","checks":["r0"]},{"title":"Forgetting ties","why":"A LIMIT 1 OFFSET 1 approach returns 98000 again for Data.","fix":"Add: \"Second-highest distinct salary; ties count once.\"","checks":["r1"]},{"title":"Leaving the output open","why":"The AI picks its own column names.","fix":"Add: \"Columns department and second_salary.\"","checks":["r2"]},{"title":"Getting a menu of options","why":"The AI likes to offer two or three versions of the query, so you're left to pick and test them yourself.","fix":"Add: \"Give one query only, in one code block, no alternatives.\"","checks":["codeBlocks","maxCodeLines"]},{"title":"Getting a tutorial","why":"By default the AI explains every part of the code at length, which you then have to scroll past.","fix":"Add: \"After the code, one line under 12 words. Nothing else.\"","checks":["maxProseWords"]}],"promptHints":[{"label":"Names the version","patterns":["5\\.7","no window","older mysql"]},{"label":"Handles ties","patterns":["distinct","ties?"]},{"label":"Sets the columns","patterns":["second_salary"]},{"label":"Asks for one answer only","patterns":["\\b(one|single|1)\\s+(query|code block|block|version)","only one","no (alternatives|options|other versions)"]},{"label":"Caps the code length","patterns":["\\d+\\s*lines","at most","under \\d+","short"]}],"hint":"The packet's answer key uses a newer database than the real test. Tell the AI which version you'll face.","rubricLabels":["Uses no window functions (no DENSE_RANK, RANK, ROW_NUMBER or OVER) and no WITH clause, since MySQL 5.7 lacks them","Finds the second-highest distinct salary per department, such as MAX(salary) where salary is below that department's MAX (a correlated subquery), so Data gives 91000 even with the tie at 98000","Returns columns named exactly department and second_salary, one row per department"]}