sql query - to queries one result set

Posted on 2015-01-20
Last Modified: 2015-01-20
I want to run two queries with one result set so i might have

Select firstname from Names
Select Company name from Company

With the result being

firstnames company

name1           abc comp
name 2         wel company
Question by:vbnetcoder
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 26

Expert Comment

by:Shaun Kline
ID: 40560158
Do you have a common field between the two tables? Without it, your results will be a match of every first name with every company name.

Author Comment

ID: 40560172
long story but that is fine.
LVL 74

Accepted Solution

sdstuber earned 500 total points
ID: 40560207
select firstname,company from names,company
LVL 50

Expert Comment

ID: 40560485
just to be clear
Select firstname from Names
Select Company name from Company
company name

what are you desiring?
firstname company
tom           ibm
dick           walmart
harry        enron


if you don't have  linking columns between the two tables then that is impossible to guarantee as a result set.... (a consistent , reproducible one ...)

the select firstname,company from [name],company solution
will return 9 rows with the above data not 3....

if you want to just match up the individual resultsets and display them side by side then this format may work for you

select a.* ,b.*
   from (select .....
                 ,row_number() over ( order by .... ) as rn
               from names) as a
  full outer join
          (select ....
             ,row_number() over ( order by ... ) as rn1
           from company) as b
  on a.rn=b.rn1
 order by coalesce(a.rn,b.rn1) asc

using the row_number() windowing function ...
the over clause can either be empty e.g  over ()    or you can specify an order by clause to determine the sequence used to determine the row sequencing....
over()  will be a "random" sequence (although likely to be physical table order)

does this help?

for more assistance please confirm which DBMS and version you are using...

Author Closing Comment

ID: 40560498

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Throw Error 7 35
Access #Deleted data 20 43
SQL Server In place upgrade from 2012 to 2014 12 22
Error in sql query statment. 21 47
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question