At First setup two database connections in config/database.php. By default you will see the following one.
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = '';
$db['default']['username'] = '';
$db['default']['password'] = '';
$db['default']['database'] = '';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
Add another db connection settings as $db['another_db'] as following
$db['another_db']['hostname'] = '';
$db['another_db']['username'] = '';
$db['another_db']['password'] = '';
$db['another_db']['database'] = '';
$db['another_db']['dbdriver'] = 'mysql';
$db['another_db']['dbprefix'] = '';
$db['another_db']['pconnect'] = TRUE;
$db['another_db']['db_debug'] = TRUE;
$db['another_db']['cache_on'] = FALSE;
$db['another_db']['cachedir'] = '';
$db['another_db']['char_set'] = 'utf8';
$db['another_db']['dbcollat'] = 'utf8_general_ci';
$db['another_db']['swap_pre'] = '';
$db['another_db']['autoinit'] = TRUE;
$db['another_db']['stricton'] = FALSE;
Then for the default database, you just do the normal query after loading the database.
$this->load->database();
$this->db->query($sql);
$this->db->result();
For loading another_db and making query,
$another_db= $this->load->database('another_db', TRUE);
$another_db->query($sql);
$another_db->result();