Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Databases

sql query

Suppose there is a database with two tables namely Loans and Billpayment.

For the Loans table I have the following columns: loanId, clientId(primary key), DisbursementDate, applicationDate, approvalDate, loanNumber, loanAmount, interestRate

For the Billpayment table I have the following columns: billId(primary), customerId(foreign), billAmount, payment_status[success/fail] billDate

note: billdate in timestamp

bill is considered paid if payement status is 'success'

1 )How can I get the clients that had a bill payment in March 2018, 2) show the number of those clients that made at least one bill payment in the remaining months of 2018, 3) show whether or not the bill payment clients have a loan within 2018 or not

1 Answer

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,736 Points

You'll want to use a join. Something like this:

SELECT * FROM Loans 
    INNER JOIN Billpayment
    ON Loans. clientId = Billpayment.customerId
  /* some WHERE conditions... /*

You also may need to first create some subqueries or common table expressions to narrow down the results of one table before you join it. So you could have a subquery for "clients that had a bill payment in March 2018" and "clients that made at least one bill payment in the remaining months of 2018" and then join from that subquery.

thank you brendan ..but the approach i took is to create a temp table for customers that had a billpayement in march and futher more i used the IN clause to get the number of those clients that made at least one bill payment in the remaining months of 2018..but trying to show whether or not the bill payment clients have a loan within 2018 or not is where i keep getting stuck.. please can u kindly explain further with the approach u have taken

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,736 Points

Are you using a join anywhere?

It's hard for me to figure out the details without access to the data. The SQL Track here at Treehouse is very good. I also recommend this course on Codecademy: Analyzing Business Metrics.