/* Using an inline view, rather than a physical table or view */
/* this is an example where the subquery is part of the FROM clause */

SELECT description, amount, ROUND(amount / total_payment * 100, 2) AS percent
FROM   payment   p  CROSS JOIN
       (SELECT  SUM(amount) AS total_payment  FROM payment)  t
ORDER BY percent DESC;



SELECT student_id, fullname, length(fullname)
FROM	
	(SELECT student_id, CONCAT(fname, lname) as fullname
         FROM student) v 
WHERE fullname LIKE '%an%';



SELECT student_id, fullname, length(fullname), course_id
FROM
	(SELECT student_id, CONCAT(fname, lname) as fullname, ssn 
	 FROM student) v 
JOIN class c
  ON v.ssn = c.stu_ssn
WHERE LENGTH(fullname) > 10;



SELECT o.payment_num, o.vendor, o.amount, o.pay_date, description
  FROM payment o  JOIN
       (SELECT vendor, MAX(pay_date) max_date 
	      FROM payment 
	     GROUP BY vendor)  i
    ON o.vendor   = i.vendor
 WHERE o.pay_date = i.max_date;