using having in where clause - sql

If I have the following table:
my_table:
varchar name
int score
and I want to do the following query:
select name from my_table group by name having count(*)>3
union
select name from my_table where name like '%xyz'
Is there a way doing this query without a union?
To be clear: I want all names which have more than three entries in the table OR answer the like clause '%xyz'.

The below query will only grab names that match the like clause before grouping:
SELECT name,count(*) as num FROM my_table GROUP BY name HAVING num > 3 OR name like '%xyz'
EDIT: The above query has been altered to allow for either the name or the num clauses to cause the row to be accepted.

Unions are only used when linking two tables.
I think you are looking for something like this?
SELECT name,count(*) as num
FROM my_table
GROUP BY name
HAVING num > 3 OR name like '%xyz'
http://sqlfiddle.com/#!9/1b6a0/2/0
Edited to match OP's question after reading intended results.

select name,count(*) as num
from my_table
group by name having num>3
where name like '%xyz'

Related

SQL delete rows with same value in column

I have column "name" with the same values.
For example:
select * table WHERE name Like 'Kate'
Result: 2 rows with this name.
I need leave only one row that has this name (random) in such rows.
And don't show other entries that have the same names.
How can I do this? Thanks.
you can use limit when you delete
DELETE FROM table WHERE name Like 'Kate' limit 1
delete from QQNAMES
where name like 'Kate%'
AND ID <> (select id from QQNAMES
where name like 'Kate%'
and ROWNum <=1);
Will delete all names in 'Kate%' pattern except the first one
Note: Not all database systems support the SELECT TOP clause. MySQL supports the LIMIT clause to select a limited number of records, while Oracle uses ROWNUM. from: w3schools
Try something like that;
delete from table where Id IN(
select Id from table WHERE name Like 'Kate' limit 1)

GROUP BY in Informix (11.5)

Following Example Table Structure:
NR1 | NR2 | PRENAME | LASTNAME
If i query all 4 fields of this table and grouping it´s first 2 fields (NR1,NR2) in mysql,
i can do something like this:
SELECT NR1,NR2,PRENAME,LASTNAME FROM tbl GROUP BY NR1,NR1
But this won´t work in informix.
INFORMIX ERROR: the column (PRENAME) must be in the group by list
After reading some Topics at google, it is an "Informix feature" that all Selected Columns has to be in the Grouping List.
But if i will do that, the result is not that result, that i wish to have.
If i use
DISTINCT
instead GROUP BY the result is similar false, because i can not put the DISTINCTfunction only to column 1 and 2.
So: How can i make a "MYSQL GROUP BY" function ?
Your original syntax is suitable in one database -- MySQL. And, that database says that the results of the non-aggregated columns come from indeterminate rows. So, an equivalent query is just to use MIN() or MAX():
SELECT NR1, NR2, MIN(PRENAME), MIN(LASTNAME)
FROM tbl
GROUP BY NR1, NR1;
My guess is that you want an arbitrary value from just one row. I'd be inclined to concatenate them:
SELECT NR1, NR2, MIN(PRENAME || ' ' || LASTNAME)
FROM tbl
GROUP BY NR1, NR1;

SQL select column value with biggest number of duplicates

I am having a problem I can't seem to solve. I have a data table that looks like this:
Example:
http://i.stack.imgur.com/tbKEk.png
I need to select the ID_JOB value which is duplicated the most. In this particular example it would be ID_JOB = 1.
Adapt this to your specific SQL implementation. Substitute [job_table] with the table you are querying.
SELECT TOP 1 ID_JOB
FROM job_table
GROUP BY ID_JOB
ORDER BY COUNT(*) DESC
You may need to add more ORDER BY logic in case a count "ties".

SQL HELP: How to get the 2 largest values from the table

I would like to select the top two int values in my table
For example lets say my table looks like this
(Name,Int)
(a,1)
(b,2)
(c,2)
(d,5)
My result query would return
d, b, and c
I am not sure how i would approach this, any suggestions?
2 or 3 values? Question says both???
select top 3 Name
from MyTable
order by IntCol desc
Is this homework? [Note: naming a column 'Int' is a very poor choice. Choose something meaningfull]
It depends on different rdbms.
You can always execute this sql:
select name from tab order by int desc limit 3;
In rdbms like oracle, it becomes more difficult
select * from (
select name from tab order by int
)
where rownum<=3;

sqlite get records with same column value

I have a SQLite DB that has people LastName, FirstName, Department and I need to make a query that shows me any people with the same First & Last Names. I've found the following statement that supposedly does what I want for a single field, however it doesn't seem to work for me when I try to use it to pull all records with just the last name being the same. How can I do this?
Select myField From myTable Group by myField Where count(myField)>1
try:
Select
firstname,LastName,Count(*)
From myTable
Group by firstname,LastName
HAVING Count(*)>1
GROUP BY combines rows where the named values are the same.
HAVING removes groups that do not meet the condition.
The above query will list the first and last names, along with a count of duplicates for all first/last names that actually have duplicates.
Firstly, you need to use HAVING, not WHERE to qualify the GROUPed BY result:
SELECT myField FROM myTable GROUP BY myField HAVING COUNT(myField) > 1
Secondly, you can extend this to multiple columns like this:
SELECT MyCol1, MyCol2 FROM MyTable
GROUP BY MyCol1, MyCol2
HAVING COUNT(*) > 1