SQL Interview Questions and Answers
Enhance your SQL skills and excel in your data analyst or data scientist interviews with our comprehensive collection of SQL Interview Questions and Answers. Click on the topic below to see the calculation
SELECT name
FROM employee
ORDER BY salary DESC
LIMIT 1 OFFSET 2;
This query will first order the employee table by salary in descending order. Then, it will return the first row that is offset by 2. This will be the third highest employee name.
DATEDIFF('month',[Date],Today()) > 0 AND DATEDIFF('year',[Date],Today())= 0
For example, if today's date is 11th June 2022. This boolean calculation returns true for the period starting from 1st Jan 2022 to 31st May 2022
DATEDIFF('year',[Date],TODAY())=1 AND [Date]<=DATEADD('year',-1,TODAY())
For example, if today's date is 11th June 2022. This boolean calculation returns true for the period starting from 1st Jan 2021 to 11th June 2021
DATEDIFF('quarter',[Date],TODAY())=0 AND [Date]<=TODAY()
For example, if today's date is 11th June 2022. This boolean calculation returns true for the period starting from 1st Apr 2022 to 11th June 2022
DATEDIFF('quarter',[Date],TODAY())=1 AND [Date]<=DATEADD('quarter',-1,TODAY())
For example, if today's date is 11th June 2022. This boolean calculation returns true for the period starting from 1st Jan 2022 to 11th Mar 2022
DATEDIFF('quarter',[Date],TODAY())=4 AND <[Date]<=DATEADD('year',-1,TODAY())
For example, if today's date is 11th June 2022. This boolean calculation returns true for the period starting from 1st Apr 2021 to 11th June 2021
[Date] <= TODAY() AND DATETRUNC( "month", [Date]) = DATETRUNC("month", TODAY() )
For example, if today's date is 11th June 2022. This boolean calculation returns true for the period starting from 1st June 2022 to 11th June 2022
Method 1: DATEDIFF('month',[Date],TODAY())=1 AND DAY([Date])<=DAY(TODAY())
Method 2: DATEDIFF('month',[Date],TODAY())=1 AND [Date]<=DATEADD('month',-1,TODAY())
For example, if today's date is 11th June 2022. This boolean calculation returns true for the period starting from 1st May 2022 to 11th May 2022
DATEDIFF('month',[Date],TODAY())=12 AND [Date]<=DATEADD('year',-1,TODAY())
For example, if today's date is 11th June 2022. This boolean calculation returns true for the period starting from 1st June 2021 to 11th June 2021
Method 1: [Date] >= DATEADD('day',-30,TODAY()) AND [Date] <= TODAY()
Method 2: DATEDIFF('day',[Date],TODAY())<=30 AND [Date] <= TODAY()
For example, if today's date is 16th June 2022. This boolean calculation returns true for the period starting from 17th may 2022 to 16th June 2022
Step 1: Calculation to date difference in seconds DATEDIFF('second',[Open Date],([Closed Date]))
Step 2: Avg Duration Calculation IIF([Seconds] % 60 == 60,0,[Seconds] % 60)// seconds
+ IIF(INT([Seconds]/60) %60 == 60, 0, INT([Seconds]/60) %60) * 100 //minutes
+ IIF(INT([Seconds]/3600) % 24 == 0, 0, INT([Seconds]/3600) % 24) * 10000 //hours
+ INT([Seconds]/86400) * 1000000 // days
Note: Here [Seconds] is the name of the calculation from step 1
DATEDIFF('year',[Date],TODAY())=0
For example, if today's date is 11th June 2022. This boolean calculation returns true for the period starting from 1st Jan 2022 to 31st Dec 2022
DATEDIFF('year',[Date],TODAY())=1
For example, if today's date is 11th June 2022. This boolean calculation returns true for the period starting from 1st Jan 2021 to 31st Dec 2021. (Assuming there is no data for future dates)