How to use a subquery in a SELECT statement (SQL Server/MySQL)

Often I need to produce a query over a parent table and get a total of values from a child table. This can be accomplished with a single query that utilizes a subquery within the SELECT statement. In the following example, we’re selecting parent values from a table named tbl_loans and utilizing a subquery to select the total of the amount paid toward principal from a child table named tbl_loanpayments. For each row in the table tbl_loans, there could be multiple child rows in the table tbl_loanpayments. We want the amount paid to the principal for all loan payment rows that have an amount paid not equal zero.
The following example will work in SQL Server or MySQL.
Continue reading “SELECT Statement Subquery Example (MySQL/SQL Server)”

There have been many cases where I’ve needed to display various child data pertaining to a parent record. Rather than clutter the page with all the information at once, I like to present the data in tabbed content.
Pie charts are a great way to display data in an appealing manner. Google charts are easy to implement and offer the user a colorful, interactive experience. The user can hover over sections of the chart to reveal specifics. The following example shows how to render a very simple Google donut style pie chart with two simple values. Our chart will show loan balance amount and loan paid amount effectively displaying the progress made against the loan.