w3resource
SQL exercises

SQL: Using precedence with specified condition

SQL Boolean Operator Statement: Exercise-10 with Solution

Write a SQL query to display order number, purchase amount, the achieved and unachieved percentage (%) for those order which exceeds the 50% of the target value of 6000.

Sample table: orders


Sample Solution :

SELECT ord_no,purch_amt, 
(100*purch_amt)/6000 AS "Achieved %", 
(100*(6000-purch_amt)/6000) AS "Unachieved %" 
FROM  orders 
WHERE (100*purch_amt)/6000>50;

Output of the Query:

ord_no	purch_amt	Achieved %			Unachieved %
70008	5760.00		96.0000000000000000	4.0000000000000000
70013	3045.60		50.7600000000000000	49.2400000000000000

Explanation :

Syntax of display order number, purchase amount, the achieved and unachieved percentage (%) for those order which exceeds the 50% of target value of 6000

Pictorial presentation :

Result of display order number, purchase amount, the achieved and unachieved percentage (%) for those order which exceeds the 50% of target value of 6000

Query Visualization:

Duration:

Query visualization of Using precedence with specified condition - Duration

Rows:

Query visualization of Using precedence with specified condition - Rows

Cost:

Query visualization of Using precedence with specified condition - Cost

 

Practice Online


Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a SQL statement where i) order dates are anything but 2012-08-17, or customer id is not greater than 3005 ii) and purchase amount is not below 1000.
Next: Write a query in SQL to find the data of employees whose last name is Dosni or Mardy.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming