DataBase
138 subscribers
1.82K photos
3 videos
3 files
57 links
Contact: java.response.email@gmail.com

Link: @database_posts

This channel covers the concepts of database using Oracle, PostgreSQL, MySQL, MongoDB and IBM db2. Enjoy!
Download Telegram
Scalar subqueries return one column and at most one row. You can replace a column with a scalar subquery in most cases.

For example, to return a count of the number of bricks matching each colour, you could do the following
#Oracle
#Scalar_Subqueries
DataBase
Subqueries: Databases for Developers Prerequisite SQL #Oracle
Note the colours with no matching bricks return null. To show zero instead, you can use NVL or coalesce. This needs to go around the whole subquery.
#Oracle
#Scalar_Subqueries_2
DataBase
Subqueries: Databases for Developers Prerequisite SQL #Oracle
You also need to join bricks to colours in the subquery. If you don't, it will return four rows (one for each different value for colour in bricks). This leads to an ORA-01427 error
#Oracle
#Scalar_Subqueries_3
DataBase
Subqueries: Databases for Developers Prerequisite SQL #Oracle
Usually you will correlate a scalar subquery with a parent table to give the correct answer.

You can also use scalar subqueries in your having clause. So instead of a join, you could write the query in part 1 to find those bricks you have less than the minimum needed like so
#Oracle
#Scalar_Subqueries_4