Menu

Post image 1
Post image 2
1 / 2
0

Subqueries and CTEs: SQL Gets Readable

DEV Community·Akhilesh·about 1 month ago
#kSYbkOS0
#subqueries#sql#database#ai#dept_id#salary
Reading 0:00
15s threshold

You learned joins. You can connect tables. Now you have a problem that requires multiple steps. First find the average salary per department. Then find employees who earn above their department average. One query cannot do this directly because you need the result of one calculation as input to another. Two solutions exist. Subqueries nest one query inside another. CTEs give each step a name and build them sequentially. Both solve the same problem. CTEs do it without making you want to delete your laptop. Subqueries: A Query Inside a Query A subquery is a SELECT statement wrapped in parentheses and embedded inside another query. import sqlite3 import pandas as pd conn = sqlite3 . connect ( " company.db " ) conn .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More