Use distinct keyword with two tables - sql

I want to take unique values using DISTINCT keyword. I have two more tables.
Table Names:
(T1)Cand_details
Locationofwork
(T2)requirement_details
Locationofposting
I want to select these two table values by using keyword distinct. Is this possible?

select LocationOfWork
from cand_details
union
select Locationofposting
from requirement_details;
A UNION operator serves to combine data from multiple SELECT statements.
In this case, without the ALL keyword (UNION ALL), the UNION operator includes a DISTINCT function which will give you the unique locations across both tables.

Something like this?
SELECT DISTINCT Locationofwork, Locationofposting FROM Cand_details, Locationofposting
You should relate both tables with common fields if they have.

Select Locationofwork as "LOC"
From Cand_details
UNION Select Locationofposting as "LOC"
FROM requirement_details

Yes this is easily possible. You can encapsulate as many feilds as you like in the distinct ( ) clause.

Related

Combine results of two table in SQL

i have two tables one contain computer_science_book and Mechanical_engineering_book i want the result which contail all books of computer_science_book and Mechanical_engineering_book .
give me suggetions.
You can use union all for that:
select * from computer_science_book
union all
select * from Mechanical_engineering_book
You can use the UNION operator:
select columns from computer_science_books
UNION
select columns from mechanical_eng_books
Note that the structure of the results from computer_science_books needs to match the structure of results from mechanical_eng_books. The reason is that, as asked for, there is only one result table. There is also the UNION ALL operator. The difference is that UNION removes duplicates whereas UNION ALL returns all combined rows.

how to combine the two queries sql?

Good day.
I have two sql query:
First:
SELECT name FROM
TableOne
WHERE city='3452' AND firm='49581' AND service='2'
Group by name
Second:
SELECT name FROM
TableTwo
WHERE city='3452' AND firm='49581' AND service='2'
Group by name
Tell me please how to combine the two queries sql and select all name (with group by name) from two tables ?
You can use UNION ALL http://sqltutorials.blogspot.com/2007/06/sql-union-all.html
SELECT name
FROM (
SELECT name
FROM TableOne
WHERE city='3452' AND firm='49581' AND service='2'
UNION ALL
SELECT name
FROM TableTwo
WHERE city='3452' AND firm='49581' AND service='2' ) x
GROUP BY name
Just use UNION operator between two queries. According to answer in this similar question:
You may be looking at using a UNION in you query:
Select * from a UNION Select * from b Note:
It is better practice to
qualify your column names instead of using the * reference. This would
also make the query still useful if your two tables underwent schema
changes but you still wanted to pull back all the data the two tables
had in common.
Union will rule out same objects. To avoid that you can use UNION ALL instead.
Well, "combine" can mean a lot of things, but I'm guessing you want something like this:
SELECT name
FROM (
SELECT name, city, firm, service FROM TableOne
UNION ALL
SELECT name, city, firm, service FROM TableTwo
) Q
WHERE city='3452' AND firm='49581' AND service='2'
GROUP BY name
There is no need to use UNION (without ALL), since GROUP BY will remove the duplicates anyway.
BTW, are all these fields used in the WHERE clause really strings? If not, you'd want to remove single quotes as appropriate. It's a bit suspicious that you are using string literals that contain only numbers.
I believe this syntax is a little simpler and thus easier to maintain. The comma in line two does a join.
SELECT name FROM
TableOne, TableTwo
WHERE city='3452' AND firm='49581' AND service='2'
Group by name

Enter multiple rows explicitly in single select statement

I am testing a query and want to use SELECT to get some initial data like so:
SELECT 1,2,3
UNION
SELECT 2,3,4
Is there a syntax to fold these two selects into one or do I have to use a UNION statement for each row?
You can use values clause
select t.* from (values(1,2,3), (2,3,4)) as t(col1,col2,col3)
If you want to display the result as one result set then you have to use union/union all

Is it possible for a subquery to return two values?

Is it possible for a subquery to return two values onto the outer query?
Such as:
SELECT 1,
(SELECT COUNT(*), MAX(*) FROM test_table WHERE test=123)
FROM another_table
Or is there a better way to do this?
If you use the subquery in the FROM clause rather than the field list, then you can treat the output as a table and refer to the separate columns.
You are just selecting numbers as results so couldn't you just do:
SELECT 1, COUNT(*), MAX(*) FROM test_table WHERE test=123
Not possible
mysql> select 1, (select 2, 3) from dual;
ERROR 1241 (21000): Operand should contain 1 column(s)
If you are dealing with two tables and you what the results in one line, you should preform a join.
Hmm, it depends on what exactly you want to do with the data, you can join two tables using JOIN syntax, and one of the tables can actually be a subquery. I think that is probably what you want.
I'm not even user what your current query will do..
Documentation:
http://dev.mysql.com/doc/refman/5.0/en/join.html

The used SELECT statements have a different number of columns

For examples I don't know how many rows in each table are and I try to do like this:
SELECT * FROM members
UNION
SELECT * FROM inventory
What can I put to the second SELECT instead of * to remove this error without adding NULL's?
Put the columns names explicitly rather than *, and make sure the number of columns and data types match for the same column in each select.
Update:
I really don't think you want to be UNIONing those tables, based on the tables names. They don't seem to contain related data. If you post your schema and describe what you are trying to achieve it is likely we can provide better help.
you could do
SELECT *
from members
UNION
SELECT inventory.*, 'dummy1' AS membersCol1, 'dummy2' AS membersCol2
from inventory;
Where membersCol1, membersCol12, etc... are the names of columns from members that are not in inventory. That way both queries in the union will have the same columns (Assuming that all the columns in inventory are the same as in members which seems very strange to me... but hey, it's your schema).
UPDATE:
As HLGEM pointed out, this will only work if inventory has columns with the same names as members, and in the same order. Naming all the columns explicitly is the best idea, but since I don't know the names I can't exactly do that. If I did, it might look something like this:
SELECT id, name, member_role, member_type
from members
UNION
SELECT id, name, '(dummy for union)' AS member_role, '(dummy for union)' AS member_type
from inventory;
I don't like using NULL for dummy values because then it's not always clear which part of the union a record came from - using 'dummy' makes it clear that the record is from the part of the union that didn't have that record (though sometimes this might not matter). The very idea of unioning these two tables seems very strange to me because I very much doubt they'd have more than 1 or 2 columns with the same name, but you asked the question in such a way that I imagine in your scenario this somehow makes sense.
Are you sure you don't want a join instead? It is unlikely that UNOIN will give you what you want given the table names.
Try this
(SELECT * FROM members) ;
(SELECT * FROM inventory);
Just add semicolons after both the select statements and don't use union or anything else. This solved my error.
I don't know how many rows in each table
Are you sure this isn't what you want?
SELECT 'members' AS TableName, Count(*) AS Cnt FROM members
UNION ALL
SELECT 'inventory', Count(*) FROM inventory
Each SELECT statement within the MySQL UNION ALL operator must have the same number of fields in the result sets with similar data types
Visit https://www.techonthenet.com/mysql/union_all.php