SQL putting text in query output
Sometimes, 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.
Example:
Sample table: agents+------------+----------------------+--------------------+------------+-----------------+---------+ | AGENT_CODE | AGENT_NAME | WORKING_AREA | COMMISSION | PHONE_NO | COUNTRY | +------------+----------------------+--------------------+------------+-----------------+---------+ | A007 | Ramasundar | Bangalore | 0.15 | 077-25814763 | | | A003 | Alex | London | 0.13 | 075-12458969 | | | A008 | Alford | New York | 0.12 | 044-25874365 | | | A011 | Ravi Kumar | Bangalore | 0.15 | 077-45625874 | | | A010 | Santakumar | Chennai | 0.14 | 007-22388644 | | | A012 | Lucida | San Jose | 0.12 | 044-52981425 | | | A005 | Anderson | Brisban | 0.13 | 045-21447739 | | | A001 | Subbarao | Bangalore | 0.14 | 077-12346674 | | | A002 | Mukesh | Mumbai | 0.11 | 029-12358964 | | | A006 | McDen | London | 0.15 | 078-22255588 | | | A004 | Ivan | Torento | 0.15 | 008-22544166 | | | A009 | Benjamin | Hampshair | 0.11 | 008-22536178 | | +------------+----------------------+--------------------+------------+-----------------+---------+
To get a formatted output with user defined column( % ) along with the 'agents' table with the following condition -
1. commission must be more than .14,
the following SQL statement can be used:
SQL Code:
-- Selecting columns agent_code, agent_name, working_area, ' % ', and commission
SELECT agent_code, agent_name, working_area, ' % ', commission
-- From the agents table
FROM agents
-- Filtering rows to retain only those where commission is greater than 0.14
WHERE commission > 0.14;
Explanation:
- This SQL code selects specific columns from the agents table where the commission is greater than 0.14.
- The SELECT statement retrieves data from the specified columns: agent_code, agent_name, working_area, and commission.
- The FROM clause specifies the table from which the data will be retrieved, in this case, the agents table.
- The WHERE clause filters the rows to retain only those where the commission column's value is greater than 0.14.
- The output will include the agent code, agent name, working area, a placeholder for percentage (%), and the commission value for agents whose commission is greater than 0.14.
Relational Algebra Expression:
Relational Algebra Tree:
Output:
AGENT_CODE AGENT_NAME WORKING_AREA '%' COMMISSION ---------- ----------------------------------------------------- ----- ---------- A007 Ramasundar Bangalore % .15 A011 Ravi Kumar Bangalore % .15 A006 McDen London % .15 A004 Ivan Torento % .15
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:
SQL Code:
-- Selecting a string literal 'For ' as the first column,
-- the count of agent names as "No.of Agent" in the second column,
-- another string literal 'Agent(s)' as the third column,
-- followed by a string literal ' in ',
-- the working_area column, the average commission, and a string literal ' % '
SELECT 'For ', count(agent_name) as "No.of Agent", 'Agent(s)', ' in ', working_area, avg(commission), ' % '
-- From the AGENTS table
FROM AGENTS
-- Grouping the results by the working_area column
GROUP BY working_area
-- Filtering the groups to retain only those with fewer than 3 agents
HAVING count(agent_name) < 3
-- Sorting the results by working_area in descending order
ORDER BY working_area DESC;
Explanation:
- This SQL code generates a report summarizing the number of agents and their average commission in each working area where the number of agents is less than 3.
- The SELECT statement retrieves data from different columns and includes string literals for formatting purposes.
- The GROUP BY clause groups the results by the working_area column.
- The HAVING clause filters the groups to retain only those with fewer than 3 agents.
- The ORDER BY clause sorts the results by working_area in descending order.
- The output will consist of rows indicating the number of agents, working area, and average commission for each working area where the number of agents is less than 3.
Output:
'FOR' No.of Agent 'AGENT(S 'IN' WORKING_AREA AVG(COMMISSION) '%' ----- ----------- -------- ------- ----------------------------------- --------------- ----- For 1 Agent(s) in Torento .15 % For 1 Agent(s) in San Jose .12 % For 1 Agent(s) in New York .12 % For 1 Agent(s) in Mumbai .11 % For 2 Agent(s) in London .14 % For 1 Agent(s) in Hampshair .11 % For 1 Agent(s) in Chennai .14 % For 1 Agent(s) in Brisban .13 %
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.
Check out our 1000+ SQL Exercises with solution and explanation to improve your skills.
Previous: Create role statement
Next: SQL order by with more columns
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/sql/advance-sql-topics/putting-text-in-query-output.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics