SQL | DIVISION

Learn via video course
FREE
View all courses
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
by Srikanth Varma
1000
5
Start Learning
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
by Srikanth Varma
1000
5
Start Learning
Topics Covered

Overview

The Division in SQL is employed for conducting integer division. It computes the result of a division operation as a quotient and omits any remainder, delivering a whole number outcome. This operator proves useful in situations that involve discrete quantities or require counting the number of times one value perfectly divides another. Suppose, In a retail store, you have a certain quantity of items in stock, and you want to determine how many can be sold in packs or bundles.

Example: If you have 25 widgets in stock and you sell them in packs of 5, you can use the DIV operator (25DIV525 DIV 5) to find that you can sell 5 packs with no leftover items.

What is a Division Operator in SQL?

The Division in SQL is used for integer division. It's used to divide one integer value by another and returns the quotient as an integer, discarding the remainder. Here's the basic syntax:

Syntax:

  • dividend is the value you want to divide.
  • divisor is the value you want to divide by.
  • table_name is the name of the table you are querying.

SQL Implementation of Division

Division in relational databases is a bit like a filter or a way to find information that matches specific criteria. Imagine you have two sets of data, like lists of people and their favourite colours. You want to find out which people have a favourite colour that matches every colour in another list. In database terms, you're looking for rows of data (people) that have a relationship with all the values in another table (colours).

Now, let's break down how you can do this:

Implementation 1:

Imagine you have two tables, R and S, where R has two columns, x and y, and S has one column, y. You want to find all the values of x in R that are associated with all the values of y in S.

  • First, you combine every value of y in S with every value of x in R (cross join). This gives you a big list (r1) of all possible combinations of x and y.
  • Next, you subtract the actual combinations from R (R(x, y)) from this big list (r1). What's left in r2 are the x values that are not associated with every value in S(y).
  • Finally, you take the x values in R and remove the x values found in r2 to get the x values that are associated with all values in S(y).

Implementation 2:

This approach uses a correlated subquery, which means it checks the relationship between each row in R and S one at a time.

  • For each row in R (as sx), it checks if there are any y values in S that are not in the corresponding row in R. If there are none, it means that x is associated with all values in S(y).

So, in simple terms, both implementations are trying to find those x values in R that are linked to all the y values in S.

Now, why is Implementation 2 more complex? It's because it checks each row individually and can be slower if you have a lot of data. Implementation 1 calculates all possible combinations first, which might be faster in some cases.

Example:

Let us take an example to understand it better:

Suppose we have two tables:

Table A: People

PersonIDName
1Alice
2Bob
3Carol
4Dave
5Eve

Table B: Hobbies

PersonIDHobby
1Reading
1Swimming
2Reading
3Swimming
4Reading
4Swimming
5Reading

Now, we want to find people who have all the listed hobbies (Reading and Swimming). This is where the division operation comes in.

Division Query

We can express this as "People who have all the hobbies in the 'Hobbies' table."

Implementation 1: Using the first implementation:

Result

PersonIDName
1Alice
4Dave

This query combines all possible combinations of people and hobbies and then removes those who don't have both Reading and Swimming as hobbies.

Implementation 2:

This query checks each person one by one and verifies if they have all the listed hobbies. The result is the same:

PersonIDName
1Alice
4Dave

So, both implementations achieve the same result: they find the people who have all the specified hobbies, which is the essence of the division operation in relational databases.

Examples

Integer Division

Suppose you have a table named integers with two integer columns numerator and divisor, and you want to perform integer division on these values:

You can perform integer division using the DIV operator:

Result

NumeratorDivisorResult
1033
723
1543

Change the Operands to a Decimal or Floating-Point Number

Suppose you have a table named sales with two columns: revenue and sales_count. Both columns are stored as integers, and you want to calculate the average revenue per sale as a decimal number:

You can change the operands to decimal numbers using CAST and then perform the calculation:

Output:

sale_iddecimal_revenuedecimal_sales_countavg_revenue_per_sale
15000.010.0500.0
27500.015.0500.0
310000.08.01250.0

Using CAST or CONVERT on Columns

Suppose you have a table named product with a column named price stored as a string (VARCHAR), and you want to convert it to both decimal and integer data types:

Applying CAST and CONVERT:

Output:

idnamedecimal_priceinteger_price
1Widget A50.9950
2Widget B75.4975
3Widget C100.75100

Conclusion

  • The Division in SQL is used for performing integer division.
  • The Division in SQL always truncates the result towards zero. This means it discards any fractional part without rounding. For example, 9DIV49 DIV 4 results in 2, not 3.
  • It's important to consider data types and potential division by zero errors when using the Division in SQL.
  • In database systems that do not support the DIV operator, functions like FLOOR(), CEIL(), or CAST/CONVERT may be used to achieve integer division.
  • The Division in SQL is not part of the SQL standard, so its availability and behaviour may differ between database management systems. Some databases, like MySQL, support it, while others may not.