Category "sql"

SQL: Count more than average HAVING vs. WHERE

I had a fairly straightforward interview question: return all countries that have more customers than the average number of customers of all cities. country ta

How can I delete rows from table A and table B if rows in table B don't exist

I have 2 tables and I need to delete rows from both tables if A.itemID does not exist in table B I've tried doing : DELETE a,b FROM A a, B b WHERE NOT E

How to remove reverse duplicates from a SQL query

Context: I have a select query where I have have link between devices. Some links have "reverse duplicates" that I would like to get rid of in the select query

SQL group by: select value where another column has its min/max

I want to group by one column, get both min and max of a second column, and (this is the tricky part!) get the value from a third column where the second column

django test app error - Got an error creating the test database: permission denied to create database

When I try to test any app with command (I noticed it when I tried to deploy myproject using fabric, which uses this command): python manage.py test appname

MYSQL rotate a returned data

I'm getting a data that return like this: | id | name | | 1 | customer1 | | 2 | customer2 | | 3 | customer3 | | 4 | customer4 | Now I want to rotate

Entire Union query returns no results if part of it returns no results

I've created the folllowing Union query, which works fine most of the time: SELECT [%$##@_Alias].[Contact ID], [%$##@_Alias].[Mailing Name] FROM (SELECT [Refer

SQL Transpose Rows to undefined number of columns

I have a table containing graduate ids, degree titles and graduation years. The table can contain any number of rows for each graduate. I would like to create

Oracle SQL query to convert a string into a comma separated string with comma after every n characters

How can we convert a string of any length into a comma separated string with comma after every n characters. I am using Oracle 10g and above. I tried with REGEX

MySQL order by field in Eloquent

When I want to define a custom sort order in a MySQL query I can do something like this: ORDER BY FIELD(language,'USD','EUR','JPN') What would be the Eloquen

How to store tree structure in sql?

Here is my schema using sqlite, I am not sure if this is a good way to create tree structure in sql as I have to traverse many time to obtain the entire tree, a

SQL ZOO List each continent and the name of the country that comes first alphabetically

Im confused why it could not be Select x.continent, x.name From world x Where x.name <= ALL (select y.name from world y where x.name=y.name) ORDER BY name

How to convert SQL Server query to MySQL (OUTER APPLY)

I'm trying to switch from SQL Server to MySQL, hence some queries need to be converted. The following SQL Server query gives my desired output: SELECT top 20 c.

SQL error "ORA-01722: invalid number"

A very easy one for someone, The following insert is giving me the ORA-01722: invalid number why? INSERT INTO CUSTOMER VALUES (1,'MALADY','Claire','27 S

Detect SQL island over multiple parameters and conditions

(PostgreSQL 8.4) I got a great introduction to SQL gaps-and-islands here on Stack Overflow but I still have a question. Many island detection CTEs are based on

MySql Query to find out first 50% of records from a Table

I am trying to fetch first 50% of records from a MySQL Table User. I know we can use limit or top for finding them but the total number of records are not fixed

Select a column with a keyword name

I have a table named 'test' It contains a column named 'AND' For that I use the following queries insert into test values ('a','abc'); insert into test value

UPDATE Query without WHERE Clause

Can the UPDATE query be used without a WHERE clause? And if so in what conditions?

Reset Identity Seed ('DBCC CHECKIDENT ... RESEED' is not working)

For my unit tests I have an automated process which builds a copy of a DB, table by table. I first of all run a SELECT INTO and add the primary key and any indi

PL/pgSQL checking if a row exists

I'm writing a function in PL/pgSQL, and I'm looking for the simplest way to check if a row exists. Right now I'm SELECTing an integer into a boolean, which does