Get an entry of certain length using LIKE - sql

select addressline1, city
from person.address
where city LIKE '[a-zA-Z][a-zA-Z][a-zA-Z][a-zA-Z][a-zA-Z] '
I want to get cities with 5 characters by operator LIKE. Is it possible?
And my result is empty.

A simple solution would be
SELECT addressline1, city FROM person.address WHERE LEN(city) = 5;
However, if you are hell bent on using LIKE, use this.
SELECT addressline1, city FROM person.address WHERE city LIKE '_____';
The LIKE solution above, however is practical only for small lengths, as your case.

Related

'Your query does not include the specified expression 'X' as part of an aggregate function' How do I fix this (beginner)

I am new to SQL, doing an assignment whereby information must be selected from two databases based on the highest 'Procurement Rate' for a specific region. I'll be honest, I have no idea what I'm doing and would appreciate any help. I have some code written & it only seems to work when I have ORDER BY written at the end, however, ORDER BY is giving me multiple results, whereas I need a single result. Im sorry if the explanation is poor, I really have little understanding in this area.
SELECT
SalesPeople.SalesPersonID,
FirstName,
LastName,
Region,
SalesRevenueYear1,
SalesRevenueYear2,
MAX (ProcurementCost)
FROM
ProductRevenueAndCosts
INNER JOIN SalesPeople
ON ProductRevenueAndCosts.SalesPersonID = SalesPeople.SalesPersonID
WHERE
ProductRevenueAndCosts.SalesPersonID =5
Try this query:
SELECT
SalesPeople.SalesPersonID,
FirstName,
LastName,
Region,
SalesRevenueYear1,
SalesRevenueYear2,
MAX (ProcurementCost)
FROM
ProductRevenueAndCosts
GROUP BY
SalesPeople.SalesPersonID,
FirstName,
LastName,
Region,
SalesRevenueYear1,
SalesRevenueYear2;
Thank you.

SQL Excercise part 2

I am stuck on another SQL exercise, here is the question:
Show the 20 biggest cities in the United States along with their rank in the state (with respect to their population) and percent of the city population in a state (call it: perc_pop_state).
Here is what i have so far. This produces the table i am looking for, but for some weird reason the percentages of city population to state population are 0's for all the states with multiple cities and 1 for all states with one city. Can anyone guide me as to what is wrong with my code.
select
city.name, city.population, city.district, rank() over (partition by district order by city.population desc), city.population / sum(city.population) over (partition by district) as perc_pop_state
from
city
inner join country on code = countrycode
where
country.name = 'United States'
order by
city.population desc
I don't know which database this is for, but most likely it's because you the figures are not decimal data type - they are whole numbers, so when you divide them the result is a whole number (0 or 1) rather than a fraction. So you should use something like this
CAST(city.population AS DECIMAL(19,4))
/
CAST(sum(city.population) AS DECIMAL(19,4))

Nested Select in SQL

I'm trying the 5th question in the Nested Select of SQL zoo (using Oracle engine) http://sqlzoo.net/wiki/SELECT_within_SELECT_Tutorial
Show the name and the population of each country in Europe. Show the
population as a percentage of the population of Germany.
I know the correct answer (given below), but something puzzled me.
SELECT name, CONCAT(ROUND(population/(SELECT population
FROM world WHERE name = 'Germany'),2)*100,'%')
FROM world WHERE continent = 'Europe'
When I run the following modified query, only one row (Albania) is returned.
SELECT name, population/(SELECT population
FROM world WHERE name = 'Germany')
FROM world WHERE continent = 'Europe'
Wondering if anyone can shed light on the inner workings of Oracle as to why only Albania is returned? Its puzzling to me why it doesn't work without ROUND().
The correct answer is actually:
SELECT name,
CONCAT(ROUND(population/(SELECT population FROM world WHERE name = 'Germany')*100,0),'%')
FROM world
WHERE continent = 'Europe'
Note the results set rounds to zero decimal spaces.
That said, I tried your exact code above and still get results for all countries:
SELECT name,
population/(SELECT population FROM world WHERE name = 'Germany')
FROM world
WHERE continent = 'Europe'
You're right to be puzzled as it should certainly return regardless of using ROUND() or not, but since I cannot recreate it, I can't explain it.
A more efficient Oracle query (that gets rid of the sub-query) is to use an analytic function:
SELECT name,
ROUND(
population
/ MAX( CASE name WHEN 'Germany' THEN population END ) OVER ()
* 100
) || '%'
FROM world;
However, sqlzoo appears to use MariaDB so you can't put that query into the website (but if you recreate the table in Oracle then you can test it).

How do I eliminate duplicate city names while adding their total count in a SQL query?

SELECT city, COUNT(pNo) Total
FROM Zip z JOIN Property p ON (z.zipcode = p.zipcode)
WHERE state = 'AL' AND rent <= 500
GROUP BY city, p.zipcode HAVING COUNT(pNo) >= 15
ORDER BY Total DESC, city;
Above is my code. My goal is to not have multiple listings of the same city, but instead have each city display once and if the city has duplicates, add their totals together. I have tried the DISTINCT clause, but it only eliminates the duplicates without doing doing any adding. I have tried sticking SUM in the code, too, but I can't quite put my finger on where it should go. Any suggestions?
The problem is you're grouping by zip code, thus creating duplicate city entries (presumably with different counts).
If you want just distinct cities, remove p.zipcode from your GROUP BY and you should be good to go.
Good luck.

SQL Server 2008: Finding strings within a table which contain the word World

I'm trying to filter my query to show only table entries with the word WORLD and also contains other country codes.
I have used the wildcard function but I am unsure as to how I can have it only return entries with both WORLD and other country codes.
SELECT *
FROM [****].[dbo].[Titles]
WHERE Territories LIKE '%world%'
Any help is appreciated
EDIT: Expected result would return all rows with both WORLD and one or more country code in the field. The territories column in this table contains both World or country codes and should bot contain both. the reason im running this query is to search for any rows with bad data.
You could try something like this
SELECT *
FROM [****].[dbo].[Titles]
WHERE Territories LIKE '%world%'
AND Territories LIKE '%countryCode%'
but I am not sure how fast that will be. If you know that the other country codes will always come after the world code, you could do something like this
SELECT *
FROM [****].[dbo].[Titles]
WHERE Territories LIKE '%world%countryCode%'
which I think should run faster.
Well if it can contain other country codes and world then you need
WHERE Territories LIKE '%world%' AND Territories LIKE '%{other country code}%'
If you are looking at world or other country code you need
WHERE Territories LIKE '%world%' OR Territories LIKE '%{other country code}%'