Some times, it is required to get an organized output from a SELECT QUERY. For that, it is better to include some user defined columns from the outside at runtime. These columns are valid only for this output. These included columns will appear as a column head and also as the contents for that column.
Sample table : agents
To get a formatted output with user defined column( % ) along with the 'agents' table with following condition -
1. commission must be more than .14,
the following SQL statement can be used :
SELECT agent_code,agent_name, working_area,' % ',commission FROM agents WHERE commission>0.14;
SQL putting text in query with group by and order by
To get a formatted output with user defined columns ('For','No.of Agent','Agent(s)','in' and '%' ) along with the 'agents' table with following condition -
1. number of agents for each 'working_area' must be less than 3,
the SQL statement can be used :
SELECT 'For ',count(agent_name)as "No.of Agent", 'Agent(s)',' in ', working_area,avg(commission),' % ' FROM AGENTS having count(agent_name)<3 GROUP BY working_area ORDER BY working_area DESC
Outputs of the said SQL statement shown here is taken by using Oracle Database 10g Express Edition.
Here is a new document which is a collection of questions with short and simple answers, useful for learning SQL as well as for interviews.