# Will learning relational algebra and calculus help me to learn SQL subqueries?

I'm finding SQL subqueries tough to understand and write.

When I ask how I can make it easier to write subqueries, the first response people give is "practice". But what do I practice? Where do I practice subqueries?

HackerRank problems are tough for my level. What are places who teach and practice subqueries?

Will learning relational algebra and calculus help me to learn SQL subqueries?

• Sep 4, 2023 at 17:01

Learning the algebraic nature of SQL will certainly help you master it.

A number of warnings though.

First, "relational algebra" when referred to in the academic sense - typically meaning the theory as devised by EF Codd across the 1970s - does not fully correspond to SQL today or explain all parts of it.

It is better to think of SQL as based upon or inspired by Codd's original work. My own opinion nowadays is that SQL also draws some conceptual inspiration from the work of KE Iverson and his language APL - a colleague of Codd's at IBM. At any rate, we are several decades along now.

Second, even when Codd's relational algebra is being taught, it often isn't taught well in my opinion.

Codd's own writings are probably the best sources on Codd, but they are relatively brief and clearly speak to expert computer scientists who would have understood a great deal tacitly - I don't know how someone unfamiliar with SQL would fare in interpreting them.

Another more particular problem about Codd's RA is that it is often not taught well in a way that actual or would-be SQL practitioners would find useful, and will often make claims about RA which are false and misleading about SQL.

Most prominent amongst these, in my opinion, are the claims that SQL works with sets, and that those sets are (by definition) unordered. In fact, SQL works with so-called "bags" or "multi-sets" from a theoretical perspective, and the stored data is very often ordered but SQL operators are lax and inconsistent about preserving a particular order (in ways that defy simple summary, and for reasons that require deep explanation).

Third, there are dialectical differences between different SQL engines and vendors. These dialectical differences can concern both syntax and actual functionality.

There are also a number of operators in modern SQL which have no real analogy at all in Codd's RA, but do fit better into the more general framework of Iverson.

You can find the official tutorial on SQL subqueries at: https://beginner-sql-tutorial.com/sql-subquery.htm. The explanation there seems clear enough.

However, I found that understanding both Relational Algebra and Relational Calculus to be very helpful in understanding the underlying structures behind relational databases and SQL in general.

If SQL is important to your studies or career, either or both of Relational Algebra and Relational Calculus should be a help as is some understanding of Entity Relationship design process.