LEFT JOIN in Postgres [duplicate] - postgresql

This question already has answers here:
What is the difference between single quotes and double quotes in PostgreSQL?
(3 answers)
Closed 4 years ago.
Ok what's wrong with this SQL query:
select j.* from judge as j
left join user as u on j.user_id = u.id
where u.email="john#example.com";
ERROR: column "john#example.com" does not exist

You are using double quotes where you should actually be using single quotes.
Single quotes denote values, while double quotes represent columns and tables, etc.
You can find out more about the difference here.

Related

How to use LIKE in these cases? [duplicate]

This question already has answers here:
Regular expression in PostgreSQL LIKE clause
(2 answers)
Closed 10 months ago.
There is a list of countries.
I want to get the list of the countries that:
consist of the letter U and A -or- S
consist of the letter U and any character except S.
These structures are correct for the MS SQL but don't work for Postgres
For MS SQL Server:
SELECT
country
FROM world.country
WHERE country LIKE 'U[AS]%'
SELECT
country
FROM world.country
WHERE country LIKE 'U[S]%'
For Postgres?
You can use SIMILAR TO Documentation
select * from (values ('UAA'),('MUA'),('UUA'),('USA')) as test (country)
where country SIMILAR TO 'U(A|S)%'
returns 'UAA' and 'USA'
You can use also POSIX regular expression with the same result.
select * from (values ('UAA'),('MUA'),('UUA'),('USA')) as test (country)
where country ~ '^U(A|S)'

postgreSQL: field separated by semicolon [duplicate]

This question already has answers here:
how to split a string in a column field value of a table to multiple rows in select query in postgresql
(1 answer)
Postgres split string with double quotes to multiple rows?
(1 answer)
regexp_split_to_table and row_number
(2 answers)
Closed 3 years ago.
I have a field called client_no_all:
client_no_all
14521;555555;636582142;
I want to separate the values and assign them to client_no:
client_no
14521
555555
636582142
Should I use a cursor to loop or pivot?
You can use following query.
The function string_to_array() splits a string by a delimiter into an array.
With the function unnest() you can expand an array to a set of rows.
SELECT unnest(string_to_array('1;2;3;4;5', ';')) AS val

Conditional expression, postgresql, get the result from the case [duplicate]

This question already has answers here:
Why can't I use column aliases in the next SELECT expression?
(4 answers)
Join Alias Columns SQL
(1 answer)
PostgreSQL does not accept column alias in WHERE clause
(1 answer)
Closed 4 years ago.
Looking at conditional expressions in Postgresql. To be more precise, this example.
SELECT * FROM test;
a
---
1
2
3
SELECT a,
CASE WHEN a=1 THEN 'one'
WHEN a=2 THEN 'two'
ELSE 'other'
END AS amod
FROM test;
a | amod
---+-------
1 | one
2 | two
3 | other
I cant seem to find out how to now access the result under amod(one, two and other). After searching around, I haven't been able to find a way to solve it. Any pointers on how to do it?
EDIT: What I'm looking for is to be able to SELECT amod FROM the case expression and get it out as a table.

Subquery not working with NULL data [duplicate]

This question already has answers here:
How does 'in' clause works in oracle
(5 answers)
NOT IN (subquery) producing zero rows
(2 answers)
SQL "select where not in subquery" returns no results
(12 answers)
Why is nothing selected in this select query?
(5 answers)
Correct way to use "NOT IN" Postgres
(4 answers)
Closed 5 years ago.
Today I have seen one weird issue in PostgreSQL query. In which I have 2 tables Products and Parameters.
Purpose:
It's very simple query, I just want to list all products which are not
there in parameter table.
Query:
select
id
from product_proudct
where active=True and
id not in
(
select distinct product_id from parameter_view
)
Issue:
It won't list anything if there is null value in
parameter table in product_id column. I have verified by just removing null record from the parameter table by adding where clause.
select distinct product_id from parameter_view where product_id is not null
And then it's working fine but with null it won't work, it's really
strange for me.
I would like to know the reason why subquery not working well with null ?

Update rows from another table [duplicate]

This question already has answers here:
How do I UPDATE from a SELECT in SQL Server?
(38 answers)
Closed 8 years ago.
I have a table with 2 columns, Country and Qty. the field country has distinct acronyms of all the countries. Now my job is to replace those acronyms with the actual country name. Now there is another table with acronyms and corresponding country names. I have to take the values from this second table and update the first one where Acronyms match. Please help..
UPDATE q
SET country = a.country
FROM quantity q
JOIN acronym a
ON a.acronym = q.country