public ResultSet joinTables(Map<String, List<String>> selectClause, List<List<String>> fromTables, List<NodeRelationship> whereClause, Map<String,List<String>> orderBy, List<String> nodeTables) throws MainException {
		String query = "", fromClause = "";

		// Process SELECT and FROM clause at the same time
		query += "SELECT ";
		fromClause += " FROM ";
		Iterator<List<String>>listItr = fromTables.iterator();
		while (listItr.hasNext()) {
			List<String> table       = listItr.next();
			String originalTableName = table.get(0);
			String tableName         = table.get(1);

			// Add columns of tables into SELECT clause
			List<String> columnsToSelect = selectClause.get(tableName);
			Iterator<String> columnsItr  = columnsToSelect.iterator();
			boolean isSameName = (originalTableName.equals(tableName));
			while (columnsItr.hasNext()) {
				String column = columnsItr.next();
				query += tableName + "." + column;
				// if table name is different from original table name
				if (!isSameName)
					query += " AS " + tableName + column + " ";
				if (columnsItr.hasNext())
					query += ",";
			}

			// Add columns into FROM clause
			if (isSameName)
				fromClause += tableName;
			else
				fromClause += originalTableName + " AS " + tableName;

			// if it is not the last table
			if (listItr.hasNext()) {
				query += ",";
				fromClause += ",";
			}
		}
		query += fromClause;

		// process where clause
		query += " WHERE ";
		Iterator<NodeRelationship> nrItr = whereClause.iterator();
		while (nrItr.hasNext()) {
			NodeRelationship nodeRel = nrItr.next();
			List<String> cols1 = nodeRel.getCols1();
			List<String> cols2 = nodeRel.getCols2();
			for(int i=0; i<cols1.size(); i++){
				query += nodeRel.getTable1() + "." + cols1.get(i) + "=" + nodeRel.getTable2() + "." + cols2.get(i);
				if(i!=cols1.size()-1){
					query += " AND ";
				}
			}

			// if it is not the last where clause
			if (nrItr.hasNext())
				query += " AND ";
		}

		// process order by clause
		query += " ORDER BY ";
		Iterator<String> stringItr = nodeTables.iterator();
		while (stringItr.hasNext()) {
			String table = stringItr.next();
			List<String> columnsToOrder = orderBy.get(table);
			Iterator<String> colItr = columnsToOrder.iterator();
			while (colItr.hasNext()) {
				String column = colItr.next();
				query += table + "." + column;
				// if it is not the last column of the last table to order by
				if (colItr.hasNext() || stringItr.hasNext())
					query += ",";
				else
					query += ";";
			}
		}

		logger.info("Query to execute : " + query);
		return executeQuery(query);

	}