Write an SQL statement that uses a sub-query to retrieve all the information about the guests from Brisbane?
SELECT * FROM guest
WHERE guestCity = Brisbane IN (SELECT guestCity FROM guest
WHERE guestCity = Brisbane)
But it kept saying:
1054 - Unknown column 'Brisbane' in 'where clause'
I don't know what I'm doing wrong. Can someone shed some light please?
guestNo, guestName, guestAddress, guestCity, guestState, guestPostcode
1, Bill Watson, 56 Gee Street, Brisbane, QLD, 4000
2, Sharon Stone, 64 New Drive, Sydney, NSW, 2000
3, Mark Harris, 100 Regents Park Road, Brisbane, QLD ,4000
4, Silvia Smith, 312 West Road, Melbourne, VIC, 3000
Can you please try this query . It will return list of guest who are having
guestCity as 'Brisbane'
SELECT * FROM guest
WHERE guestCity = 'Brisbane'
You don't need the sub query to return what you need.
You also need to wrap strings in single quotes
SELECT g.*
FROM guest g
WHERE g.guestCity = 'Brisbane'
Sub Query
SELECT g.*
FROM guest g
WHERE g.guestCity IN (SELECT sg.guestCity
FROM guest sg
WHERE sg.guestCity = 'Brisbane')
Related
For a school project, i need to create some databases in MS SQL Server , create some views, and manage the users acces. The thing is that our course was really empty, and i'm having trouble with some mechanics of MS SQL Server
I have a table with every player of the football world cup of 2018, and i want to take the 20 shortest players, so i just wanted to use ORDER BY Person.Height. If i use that, my result is something like that :
62 165 Panama QA19
63 165 SaudiArabia AY8
78 165 Switzerland SX23
59 166 Mexico AJ20
etc...
but i just want to take the shortest players and order them by country, not by height, so having something like that
69 168 Argentina SE18
66 168 Argentina LM15
67 166 Brazil CF22
64 169 Brazil RF18
the 20 shortest players ordered by country in an alphabetical order
This is the code i have right now, and this give me the first result
SELECT TOP (20) Person.Weight, Person.Height, Country.NameCountry,
LEFT(Person.Name, 1)+LEFT(Person.FirstName, 1)+CAST(Player.Numero AS
nvarchar(MAX)) AS Initiales
FROM Person INNER JOIN Country
ON Person.CountryId = Country.CountryID
INNER JOIN Player
ON Person.PersonID = Player.PersonID
WHERE -----------------
ORDER BY Person.Height, Country.NameCountry
If i use MIN(Person.Height) in the WHERE clause, i have this error : An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.
But i don't need any HAVING OR GROUP BY clause, so i don't really understand what i'm supposed to do.
I know that my explanation is not very clear, but if somebody could help me with that, that would be vey nice
Try with a subquery
SELECT * FROM (
SELECT Person.Weight, Person.Height, Country.NameCountry,
LEFT(Person.Name, 1)+LEFT(Person.FirstName, 1)+CAST(Player.Numero AS
nvarchar(MAX)) AS Initiales
FROM Person INNER JOIN Country
ON Person.CountryId = Country.CountryID
INNER JOIN Player
ON Person.PersonID = Player.PersonID
WHERE -----------------
ORDER BY Person.Height
LIMIT 0,20)
ORDER NameCountry
I am creating a library database and have four tables as follows;
I have been researching ways to work out the frequency in MySQL but after such as long time and misunderstanding I've decided to try get an example of how to work out the frequency on tables that I'll understand. Below are the four tables I am currently using.
I am looking to workout the loan frequency of every book that has been loaned 2 or more times. By doing this I am able to see how working out frequency would work when selecting specific values instead of all values.
From looking at my tables I would have to select the 'code' from the loan table, select all values that occur twice or more and then workout the frequency of the occurrence.
From my research I would decide to use an INNER JOIN to connect the tables, COUNT to count the number of values, GROUP BY to group the values and HAVING as WHERE may not be used. I am having trouble writing the query and continuously stumble upon errors. Could anyone use the example above to explain how they worked out the frequency of each book loaned two times or more? Thanks in advance
Table 1 - book
isbn title author
111-2-33-444444-5 Pro JavaFX Dave Smith
222-3-44-555555-6 Oracle Systems Kate Roberts
333-4-55-666666-7 Expert jQuery Mike Smith
Table 2 - copy
code isbn duration
1011 111-2-33-444444-5 21
1012 111-2-33-444444-5 14
1013 111-2-33-444444-5 7
2011 222-3-44-555555-6 21
3011 333-4-55-666666-7 7
3012 333-4-55-666666-7 14
Table 3 - student
no name school embargo
2001 Mike CMP No
2002 Andy CMP Yes
2003 Sarah ENG No
2004 Karen ENG Yes
2005 Lucy BUE No
Table 4 - loan
code no taken due return
1011 2002 2015.01.10 2015.01.31 2015.01.31
1011 2002 2015.02.05 2015.02.26 2015.02.23
1011 2003 2015.05.10 2015.05.31
1013 2003 2014.03.02 2014.03.16 2014.03.10
1013 2002 2014.08.02 2014.08.16 2014.08.16
2011 2004 2013.02.01 2013.02.22 2013.02.20
3011 2002 2015.07.03 2015.07.10
3011 2005 2014.10.10 2014.10.17 2014.10.20
You didn't specify the type of frequency, but this query calculates the number of loans per week for each book that was loaned more than once in 2014:
select b.isbn
, b.title
, count(*) / 52 -- loans/week
from loan l
join copy c
on c.code = l.code
join book b
on b.isbn = c.isbn
where '2014-01-01' <= taken and taken < '2015-01-01'
group by
b.isbn
, b.title
having count(*) > 1 -- loaned more than once
I have a table containing: ID, FEATURE_NAME (that would be a name of the city), STATE_ALPHA (two letter country identifier like 'AL' for Alabama), and POPULATION_DATA.
I need to:
find entries that have same FEATURE_NAME and STATE_ALPHA
take values for POPULATION_DATA in both(or more) appearances and add them
write down the sum in POPULATION_DATA where all addends are from.
Example:
- ID !FEATURE NAME ! STATE_ALPHA ! POPULATION DATA
- 1 Woodland WA 83
- 2 Woodland WA 5426
- 3 Vining KS 354
- 4 Vining KS 276
- 5 Vining KS 1450
What I need to get is:
- ID !FEATURE NAME ! STATE_ALPHA ! POPULATION DATA
- 1 Woodland WA 5509
- 2 Woodland WA 5509
- 3 Vining KS 2080
- 4 Vining KS 2080
- 5 Vining KS 2080
I googled for hours and dont even know where to start. Also I'll run that script on a view not on original table, I don't know does it changes anything. Please help.
You can do this with an aggregation and a join:
select t.id, t.feature_name, t.state_alpha, sumpop as Population_Data
from t join
(select feature_name, state_alpha, sum(population_data) as sumpop
from t
group by feature_name, state_alpha
) fs
on t.feature_name = fs.feature_name and
t.state_alpha = fs.state_alpha;
The aggregation sums the population (in the subquery fs). This result is joined back to the original data.
If I understand your question, a simple query should work where the table name is tablename
SELECT ID, FEATURE_NAME, STATE_ALPHA, POPULATION_DATA FROM tablename
FROM tablename
GROUP BY FEATURE_NAME, STATE_ALPHA
Using Rails 3.2. I have the following table:
name address
=======================================
Hilton Hotel New York, USA
Hilton Hotel Paris, France
Mandarin Hotel Chicago, USA
Le Meridien Hotel New York, USA
and the following query:
term = "%#{params[:term]}%"
shops = Shop.limit(10).where("name LIKE ? OR address like ?", term, term)
My expected result is this:
Search - "Hilton"
Result - "Hilton Hotel, New York, USA"; "Hilton Hotel, Paris, France"
Search - "Hilton USA"
Result - "Hilton Hotel, New York, USA"
Search - "New York"
Result - "Hilton Hotel, New York, USA"; "Le Meridien Hotel, New York, USA"
How should I rewrite my query?
Thanks.
You could use MySQL's MATCH AGAINST. There's no direct support in Rails, but you can always roll your own custom query in Rails using
YourModel.find_by_sql("SELECT ... WHERE MATCH(...) AGAINST(...)")
or a bit more Rails style (have not tested this):
YourModel.where("MATCH(...) AGAINST(...)")
For more information, have a look here: http://dev.mysql.com/doc/refman/5.1/en/fulltext-boolean.html
EDIT:
I'd split the input string by space, comma, dot, etc. and then use MATCH AGAINST to get all results. So if you have this table:
col1 | col2
-----------
row1: a c | a
row2: a d | b e
row3: b e | a d
row4: b | b c
And the user types a as input. You should do
MATCH(col1, col2) AGAINST ('a')
This will return:
row1: 'a c', 'a'
row2: 'a d', 'b e'
row3: 'b e', 'a d'
I use another workaround for this.
I create another column for_autocomplete and concatenate the name and address into it. Every time a record is created/updated, it will create/update for_autocomplete accordingly. Then I just search through the for_autocomplete column.
I have a table of cities that all share the same area code:
367 01451 Harvard Worcester Massachusetts MA 978 Eastern
368 01452 Hubbardston Worcester Massachusetts MA 978 Eastern
369 01453 Leominster Worcester Massachusetts MA 978 Eastern
The table has multiple area codes, all with multiple cities.
What I'd like to do is only select one city from each area code and delete any extra cities from duplicate area codes. What would be the best query to accomplish this?
I believe:
Mysql4: SQL for selecting one or zero record
Is coming close to what I need but didn't quite get what/how those answers were working.
Note The "978" row is the "area_code" row, table name is "zip_code".
DELETE c.*
FROM zip_code c
JOIN (
SELECT area_code, MIN(id) AS mid
FROM zip_code
GROUP BY
area_code
) co
ON c.area_code = co.area_code
AND c.id <> co.mid