Hello, I have to answer the following question to have a interview for a new job. Could you verify if my answer is correct ? I am not sure because of the timestamp data type and the CURRENT_TIMESTAMP for the variable 'login_time'. Thank you very much.
Using the table definition below and assuming such table exists with 750 million records, write an efficient SQL query (or queries) that returns the total number of logins and the number of unique profiles that logged in between January 1, 2015 and February 15, 2015, inclusively. Explain your logic.
CREATE TABLE `LoginLog` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`login_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`profile_id` int(11) DEFAULT '-1',
`mailer_id` int(10) unsigned DEFAULT '0',
`entry_point` enum('UNKNOWN','MAIN_SITE','REGISTRATION','MAILER','MOBILE') NOT NULL DEFAULT 'UNKNOWN',
PRIMARY KEY (`id`),
KEY `profile_id_idx` (`profile_id`))
ENGINE=MyISAM AUTO_INCREMENT=459520272 DEFAULT CHARSET=utf8
);
My answer are :
1- Script for total number of unique profiles that logged in between January 1, 2015 and February 15, 2015, inclusively
Select count (distinct profile_id)
From LoginLog
Where login_time is >= 2015-01-01 00 00 01 and login_time is <= 2015-02-15 23 59 59;
2-Script for returns the total number of logins between January 1, 2015 and February 15, 2015, inclusively.
-- Hypothesis : 1 line = 1 login--
Select count (Id)
From LoginLog
Where login_time is >= 2015-01-01 00 00 01 and login_time is <= 2015-02-15 23 59 59;