Demo of getting records between two dates using two tables

We will use our student table and link this table to fee payment table to test various quires to collect records between two date ranges. Here are our two tables with some sample data.
fee_ididdtamount
112013-01-08200
212013-01-10100
322013-01-24120
432013-02-12211
522013-02-07150
632013-02-06135
742013-02-14100
idnameclassmark
1John DeoFour75
2Max RuinThree85
3ArnoldThree55
4Krish StarFour60
5John MikeFour60
6Alex JohnFour55
Here is our query to display records between two date ranges
SELECT * FROM `student_fee` 
  WHERE dt BETWEEN '2013-01-09' and '2013-01-30'
We will get output displaying two records
fee_ididdtamount
212013-01-10100
322013-01-24120
Now Linking two tables we will display the records
SELECT * FROM `student_fee`,student 
  WHERE student_fee.id=student.id
  AND  dt BETWEEN '2013-01-09' and '2013-01-30'
The output is here
fee_ididdtamountidnameclassmark
212013-01-101001John DeoFour75
322013-01-241202Max RuinThree85
Try to find out total fee collected in Feb 2013
SELECT SUM(amount) FROM student_fee 
	WHERE MONTH(dt)=2 AND YEAR(dt)=2013
Ouput is
596
By using DATE_FORMAT
SELECT SUM(amount) FROM student_fee 
	WHERE DATE_FORMAT(dt,'%b-%Y')='Feb-2013';
OR
SELECT SUM(amount) FROM student_fee 
	WHERE DATE_FORMAT(dt,'%M-%Y')='February-2013';
Try to find out how much John Deo had paid in Year 2013
SELECT * FROM `student_fee`,student 
WHERE student_fee.id=student.id 
AND  dt BETWEEN '2013-01-01' and '2013-12-31'
AND student.name='John Deo'
Output
fee_ididdtamountidnameclassmarkgender
112013-01-082001John DeoFour75female
212013-01-101001John DeoFour75female
Download sql dump of student table
Download sql dump of studnet_fee table
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com
saraazee

03-06-2014

how can we retrieve the data from database by a particular month?




SQL Video Tutorials










We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer