A little lesson on MP
Higher megapixel numbers are not always better. The sensor sizes do not change, do the pixels have to get smaller. That means they don't gather as much light. The D40 sensor will likely give you better low-light performance than the sony. But compare them in the store. Set the ISO, f/stop and shutter to the same setting, and take a picture of the same subject, without the flash. See which one is more sensitive.
Besides that, if you don't print, or print 8x12 or smaller, then anything over 5-6 Megapixel is a waste.
1999 e46 328i Ti Silver / Black[retired]
2007 e90 335xi Jet Black / Black[retired]
2011 e70 X5 35d Vermillion Red / Cinnamon
2011 e92 M3 LeMans / Fox Red extended