I have a database table with name 'student' and have column 'name'.
I want to retrieve the names of students starting without a title 'miss' but starting with 'miss'.
I did:
select name from students where lower(name) like 'miss%' and name not like 'miss %'
The above query returns names with title also.
Any help will be highly appreciated.
do you maybe just need lower() around "name" the second time? For me with Postgres 9.5 your query works, if you say "and lower(name) not like 'miss %'
Related
I have a Postgres table that has names and addresses. Some of these name fields are both names of a couple -- for example, "John & Jane".
I am trying to write a query that pulls out only those rows where this is the case.
When I run this query, it selects 0 rows even though I know that they exist in the table:
SELECT count(*) FROM name_list where namefirst LIKE '%&%';
Does anyone know how to address this?
i'm using this query to look for data in a table where profile is a JSONB column
and it works but only if the name is exactly that
SELECT * FROM "users" WHERE "profile" #> '{"name":"Super User"}'
is it possible to have more flexibility like case insensitivity, wildcards and so on ?
Something like "Super%" or "super user"
I found the solution to my problem:
SELECT * FROM "users" WHERE (profile #>> '{name}') ILIKE 'super %'
I don't know if this is performing well enough but it works.
Probably it's wise to add an index to it.
I have a query to select names of employee start with 'y', then my query would be
SELECT EMP_NM FROM EMP
WHERE EMP_NM LIKE'Y%'
Say for example
I need to retrieve names of employee
names start with both 'y' and 'z', do
we have anything to suffice my
requirement? It's something like using
OR with LIKE, correct?
Your time and responses are highly appreciated.
WHERE EMP_NM LIKE 'Y%' OR EMP_NM LIKE 'Z%'
or, not sure if DB2 supports this (not even sure I have right link to documentation...)
WHERE EMP_NM LIKE '[YZ]%'
I have a table in postgresql. The following table "animals" will do to explain my problem:
name
------
tiger
cat
dog
Now I am using the following query:
SELECT
array_to_string(array_agg("name"), ', ')
FROM
animals;
The result is: "tiger, cat, dog". But I would like to sort the aggregate, before it is converted into a string. So this is the result I am hoping for:
"cat, dog, tiger".
So how can I sort an string array in postgresql 8.4 before converting it to a string. ORDER BY on the row "name" does not work and the built-in sort function processes only integer values.
Anyone a good idea, how to solve this in pure SQL?
Thanx a lot
Richard
For modern PostgreSQL (since version 9.0), you can use an ORDER BY clause in an aggregate expression:
SELECT
array_to_string(array_agg(name ORDER BY name), ', ')
FROM
animals;
Also, for your specific purpose, you can use string_agg to simplify your query:
SELECT
string_agg(name, ', ' ORDER BY name)
FROM
animals;
This will be available in PostgreSQL 9.0:
http://www.postgresql.org/docs/9.0/static/release-9-0.html, Section E.1.3.6.1. Aggregates
In the meantime, you could do something like this which may solve the problem (albeit clunky):
SELECT array_agg(animal_name)
FROM (
SELECT "name" AS animal_name
FROM animals
ORDER BY "name"
) AS sorted_animals;
Although Matthew Wood's answer is better for your case, here is a way to sort arrays in PostgreSQL 8.4 and up:
SELECT array(
SELECT unnest(array[3,2,1]) AS x ORDER BY x
);
Knowing the array and unnest functions can be handy, since it also lets you do things like "map" over an array:
SELECT array(
SELECT x*x FROM (SELECT unnest(array[1,2,3]) AS x) as subquery
);
Again, this can be yours for the price of PostgreSQL 8.4 .
Have you tried to use generate_series() on the array, and then do a SELECT...ORDER BY on that result (or just nest it inside of the SELECT) before you convert it to a string?
Still, for version 8.4, using the solution suggested by Matthew Wood, if you need to do a grouping in the outer query, the inner query should be sorted, too, for the sorting to be consistent.
SELECT family, array_agg(animal_name)
FROM (
SELECT family, "name" AS animal_name
FROM animals
ORDER BY family, "name"
) AS sorted_animals
group by family;
To update on this question, Snowflake has implemented array sorting:
SELECT
array_sort(array_agg("name")
FROM
animals;
Can also use array_sort_by to sort an object
how to make this work in mysql?
select ID,COMPANY_NAME,contact1, SUBURB, CATEGORY, PHONE from Victoria where (city in ( select suburb from allsuburbs)) and CATEGORY='Banks'
this below statement is working:
select ID,COMPANY_NAME,contact1, SUBURB, CATEGORY, PHONE from Victoria where city in ( select suburb from allsuburbs)
if I add "and" , it gives me an empty resultset,
thanks
Learn how joins work.
select
v.ID,v.COMPANY_NAME,v.contact1,v.SUBURB,v.CATEGORY,v.PHONE
from
Victoria v
inner join allsuburbs s on s.suburb = v.city
where
v.CATEGORY='Banks'
Apart from that, your query does not make a whole lot of sense.
Your table is namend Victoria, but it contains a field named city?! Do your other cities have their own table too?
You have a table named allsuburbs, but your criterion is that Victoria.city equals allsuburbs.suburb, even though a field named Victoria.suburb exists?! What's Victoria.suburb for, then?
Your table is named allsuburbs. Do you have another table that contains suburbs or is this your only one? If it is your only one, the name is redundant.
You have a field contact1. Do you have contact2...contact10 as well? Bad database design.
Why is half of your fieldnames in caps, and not all of them (or none of them)?
Oh, and the usual format for SQL is: SQL keywords in caps, the field names etc. in mixed case/lower case. Much easier to read.
I think you might have misplaced a parentheses?
.. PHONE from Victoria where
(city in ( select suburb from allsuburbs)) and CATEGORY='Banks'
I'm guessing should be:
.. PHONE from Victoria where
city in ( select suburb from allsuburbs) and CATEGORY='Banks'
Not sure if that makes more sense, but the first case is not an ok SQL-statement I believe.