Recently interviewed a number of senior BI candidates, industry experience from three to seven years, initially felt that the level of no need to prepare a pen test, in view of the leadership, on the real project to extract 5 SQL topics, here only one of the difficulty of the lower middle of the topic, put aside interview not to talk about, A few rounds of the written test is not ideal, so far no one can write logic watertight and maximize the performance of the script, the problem even has failed, if crossing think this is only anise beans anise several ways of writing problems on the drift, I would like to examine through the topic is not just the proficiency of SQL , the relative importance of the candidate's sensitivity to the data, as well as the ability to optimize script performance, goes deep to understand how the database storage engine and query optimizer work, which is the basic quality that a qualified BI practitioner should have.
The topic is this, there is an ER diagram describing data structure, here does not map, simple text described as follows, crossing a variety of brain complement the main foreign key relationship:
A membership Form (account), the field has a member ID (ACCOUNT_ID), membership card number (Account_num) ...
A trading order form (trans), field with Member ID (ACCOUNT_ID), Trading Time (trans_time), transaction amount (sales) ...
Ask for a membership card number with the first order amount exceeding 1000 and its first order amount,
Note:
1. The first order refers to a single order with the minimum transaction time for each member.
2. The membership table has 10 million records
3. If the member does not have any order or the first order amount is less than 1000, the first order amount will return 0.
Here are three examples of typical answer script for reference, candidate BI experience is three years, four years, six years, the script record style is too wayward, because the topic is not constrained DB products, so there are use of various DB private functions, with the use of the old style of join, and even the use of the CTE, the condition is full, In order to take care of crossing mood, in respect of the original intention of the principle of carding after the TSQL script as follows:
A
Select A.account_number, T.salesfrom account ajoin (select account_id, Sales, Min (trans_time) from Transgroup by Trans_ Idhaving sum (Sales) >1000) T on a.account_id=t.account_id
B
Select A.account_number, T.sales, Rank () over (partition by a.account_id ORDER by T.trans_time) Rnfrom account Ajoin Trans T on A.account_id=t.account_idwhere rn=1 and T.sales>1000group by A.account_number,t.trans_time
C
Select A.account_number, T.salesfrom account Aleft join (Select account_id,sales,row_number () over (order by Trans_time) R Nfrom Trans) T on a.account_id=t.account_id where T.rn=1 and t.sales>1000
How would you write this SQL pen question?