merged SQL table names - sql

I have a merged table from several unions and i want to know from which of those tables the results were taken, is that possible?
example...
select name from users where name like '%alex%'
union
select name from admins where name like '%alex%';
Would return lets say two rows, Alexander and Alexandra. Alexander is an admin and Alexandra is a user. How can i tell them apart?

SELECT
Name,
'Users' AS Type
FROM users
WHERE name LIKE '%alex%'
UNION
SELECT
Name,
'Admins' AS Type
FROM admins
WHERE name LIKE'%alex%'

Include a virtual column in your select that will allow you to identify the source table
select name, 'Name' as Source from users where name like '%alex%'
union select name, 'Admins' as Source from admins where name like '%alex%';

Related

SQL - Find specific value in a specific table

Say I have a table called "Team" with the following columns:
ID, MemberName ,ManagerName,Title
And I would like to retrieve all rows where a value "John" exists.
Assume "John" exists in a row for the MemberName column, and that "John" would exist in another row under the "ManagerName" column.
Please assume would have large number of columns. Greater than 50, and do would not know where the value would fall under statically.
If you need an exact match for "John" you can use following query:
Select *
From Team
Where MemberName = 'John' or ManagerName = 'John'
If you need all rows where "John" could be a part of the string then you can use like:
Select *
From Team
Where MemberName like '%John%' or ManagerName like '%John%'
Generally, you need to specify all the columns your are searching from in SQL.
SELECT * FROM Team WHERE 'John' IN (col1, col2, col3, ..., colN) ;
However that depends.
If you are using MySQL you can Search Table Data. From the MySQL Workbench right click the table , and choose Search Table Data.
If you are using PostgreSQL take a look at the following:
https://stackoverflow.com/a/52715388/10436747

select to check if a column does not contain a value in a provided array

I would like to select to check if any of the countries I have in my string array is not contained in the country table in the psql database.
Therefore I have a list of country names ARRAY['Country1','country2'.....]
and I have a table of country and I want a query to select countries that are not in this string that I will provide to the where clause
Something like this
SELECT name from country where name not in (ARRAY['Country1','country2'.....])
You were nearly there:
SELECT name
from country
where name <> ALL (ARRAY['Country1','country2'.....])

Create view with except condition

Example i want to create a view name TEST
which i already created a view before that
View name : customer
Inside customer View:
//CUSTOMER
NAME ID ADDRESS AGE SEX TELNO EMAIL
-----------------------------------------------------------------------
CHRIS 1 12321312 21 F 646885 ascs#gmail
JOHN 2 SADASDSA 23 M 5452131 asd#gmail
MAY 3 LKJLKJLKJ 32 F 645643 cxz#gmail
So i want to create a view name TEST that will store all column inside CUSTOMER but EXCEPT TELNO EMAIL.
so i used this query:
CREATE VIEW TEST AS
SELECT * FROM CUSTOMER
EXCEPT
SELECT TELNO,EMAIL FROM CUSTOMER;
But i fail to work, got errors come out. SQL command not properly end and point to EXCEPT, what's wrong?
You have to list all the columns that you want explicitly:
CREATE VIEW TEST AS
SELECT NAME, ID, ADDRESS, AGE, SEX
FROM CUSTOMER;
There is no way to exclude some columns from a * list.
EXCEPT is an operator in SQL Server. The equivalent in Oracle is MINUS. However, this works at the row level, not at the column level.
If you want to get all the columns in the table, except for those two, you can use all_tab_columns:
select column_name
from all_tab_columns
where lower(table_name) = 'customer' and
lower(column_name) not in ('telno', 'email');
You can then paste them into the select clause.

MySQL SELECT query string matching

Normally, when querying a database with SELECT, its common to want to find the records that match a given search string.
For example:
SELECT * FROM customers WHERE name LIKE '%Bob Smith%';
That query should give me all records where 'Bob Smith' appears anywhere in the name field.
What I'd like to do is the opposite.
Instead of finding all the records that have 'Bob Smith' in the name field, I want to find all the records where the name field is in 'Robert Bob Smith III, PhD.', a string argument to the query.
Just turn the LIKE around
SELECT * FROM customers
WHERE 'Robert Bob Smith III, PhD.' LIKE CONCAT('%',name,'%')
You can use regular expressions like this:
SELECT * FROM pet WHERE name REGEXP 'Bob|Smith';
Incorrect:
SELECT * FROM customers WHERE name LIKE '%Bob Smith%';
Instead:
select count(*)
from rearp.customers c
where c.name LIKE '%Bob smith.8%';
select count will just query (totals)
C will link the db.table to the names row you need this to index
LIKE should be obvs
8 will call all references in DB 8 or less (not really needed but i like neatness)

How can I write this SQL SELECT query for this table?

I have this situation in a certain table:
id | name
1 'Test'
2 'Test'
3 'Test'
How can I make a query to SELECT by distinct the name? I also need the ID column, even if I get the first occurrence of the element, e.g. "if the name column repeats, give me the first record with this repetition."
select name, MIN(ID)
from aCertainTable
group by name