SQL SELECT FROM WHERE FirstName > "Maria" - mysql

I have the following table:
First Name
Bryce
Marcellin
Caroline
Kerry
Roberto
Mary
Carol
Warren
Bonnie
Terry
Louis
Michelle
Bobby
Tony
Vic
Frank
Roberto
Jose
Doug
Brian
William
Aiden
Davis
What exactly does SELECT FirstName FROM Members WHERE FirstName > "Maria"; search for ? in particular, the WHERE statement.
It returns the names:
Roberto, Mary, Warren, Terry, Michelle, Tony, Vic, Roberto and William
I thought it was searching for FirstName strings that are longer than 5 characters but this is not the case since Tony and Vic are also returned.

It searches for terms that are in alphabetical order AFTER "Maria."
For example, with "Jack, James, Jim" if you searched for SELECT name FROM table WHERE first_name >= 'James' you would receive the results of 'James' and 'Jim', since alphabetically those two are after one another. The reason Vic, Robert, William, etc are returned is because they are alphabetically after the value of "Maria"

Maria's string length is 5 characters, so use length function to find length of individual names and check it to be greater than 5 as below:
SELECT FirstName
FROM Members
WHERE length(FirstName) > 5;
Or if your name is going to be dynamic, you could use like:
SELECT FirstName
FROM Members
WHERE length(FirstName) > length("Maria");
If you need all names that comes after Maria and length greater than 5 then use:
SELECT FirstName
FROM Members
WHERE FirstName > 'Maria';
AND length(FirstName) > length('Maria');

Related

How to find the Employee names and their supervisor names if the table doesn't have common numeric column like Employee_id or employee number in Mysql

If the table have only two columns with employee name and their supervisor column and if it doesn't have any other numeric or number column with employee_number or employee_id, then how the results can be produced. I'm not getting logic to show the results.
Code for creating table in Mysql:
CREATE TABLE DATABASE_TABLE
(
Employee_Name nvarchar(255) PRIMARY KEY,
Supervisor_Name nvarchar(255) NOT NULL
);
CREATE INDEX ix_database_table_supervisor
ON DATABASE_TABLE (Supervisor_Name);
INSERT INTO DATABASE_TABLE
(Employee_Name, Supervisor_Name) VALUES
('Alice','Dave'), ('Olive','Dave'), ('Barton','Dave')
, ('Almira','Jacob'), ('Charles','Jacob'), ('Davis','Jacob')
, ('Robert','Risha'), ('Peter','Risha'), ('Ethel','Risha')
, ('Isaac','Jospeh'), ('Sophia','Jospeh'), ('Rosa','Jospeh')
, ('Joshua','Dandy'), ('Silas','Dandy'), ('Fred','Dandy')
, ('Frank','Andrew'), ('Howard','Andrew'), ('Ralph','Andrew')
, ('Dennis','Henry'), ('Alex','Henry'), ('Floyd','Henry')
, ('Carlos','Nelson'), ('Homer','Nelson'), ('Harold','Nelson')
, ('Leo','Simon'), ('Warren','Simon'), ('Clifford','Simon')
, ('Martha','Casper'), ('Hazel','Casper'), ('Irene','Casper')
, ('Dave','Betsy'), ('Jacob','Betsy'), ('Risha','David')
, ('Jospeh','David'), ('Dandy','Phillip'), ('Andrew','Phillip')
, ('Henry','Harvey'), ('Nelson','Harvey'), ('Simon','Paul')
, ('Casper','Paul'), ('Betsy','Joe'), ('David','Joe')
, ('Phillip','Joe'), ('Harvey','Joe'), ('Paul','Joe')
It's output is:
Employee_name Supervisor_name
Frank Andrew
Howard Andrew
Ralph Andrew
Dave Betsy
Jacob Betsy
Hazel Casper
Irene Casper
Martha Casper
Fred Dandy
Joshua Dandy
Silas Dandy
Alice Dave
Barton Dave
Olive Dave
Jospeh David
Risha David
Henry Harvey
Nelson Harvey
Alex Henry
Dennis Henry
Floyd Henry
Almira Jacob
Charles Jacob
Davis Jacob
Betsy Joe
David Joe
....
The result should be in the lower to higher level of hierarchy like:
Employee_Name Supervisor_Name Higher_Supervisor Next_higher_Supervisor
Frank Andrew Phillip Joe
Howard Andrew Phillip Joe
Ralph Andrew Phillip Joe
Dave Betsy Joe no_supervisor
Jacob Betsy Joe no_supervisor
Hazel Casper Paul Joe
Irene Casper Paul Joe
Martha Casper Paul Joe
For Eg: Frank's supervisor is Andrew, Andrew's supervisor is Phillip, Phillip's supervisor is Joe
For Eg: Dave's supervisor is Betsy, Betsy's supervisor is Joe, and Joe doesn't have any supervisor so no_supervisor should be displayed.
For Eg: Hazel's supervisor is Casper, Casper's supervisor's is Paul, and Paul's Supervisor is Joe should be displayed in the order format
For this particular set of data, it can LEFT JOIN the table itself to get the expected results
SELECT a.Employee_Name, a.Supervisor_Name, b.Supervisor_Name, c.Supervisor_Name
FROM DATABASE_TABLE a
LEFT JOIN DATABASE_TABLE b ON a.Supervisor_Name = b.Employee_Name
LEFT JOIN DATABASE_TABLE c ON b.Supervisor_Name = c.Employee_Name
If hierarchy depth is unknown, which means the number of columns is unknown, it's more complicated. It is still possible by using recursive CTE to find the depth and generate dynamic SQL.

DISTINCT ON field is not available on MySQL. Which equivalent do you use?

Let's say I've got a model:
class Person(models.Model):
id = models.AutoField(primary_key=True)
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
I'd like, by searching the first letter of a first_name, to get a list of Person. But each one must have a different last_name.
For example:
searching M... in the list of:
Frank Edmond
Marc Thomas
Matthew Ronald
Matthew Smith
Matthew Thomas
Richard Thomas
will give you
Marc Thomas
Matthew Ronald
Matthew Smith
In short, how to translate in MySQL:
Person.objects.filter(first_name__startswith='M').distinct('last_name')
In MySQL
SELECT DISTINCT <table>.last_name FROM <table> WHERE <table>.first_name LIKE 'M%'
Replace <table> with your table name.

Query for finding similar interests together

Name Place visited
Ash New york
Bob New york
Ash Chicago
Bob Chicago
Carl Chicago
Carl Detroit
Dan Detroit
Above is the sample table. The output should be two names who visited place together. I.e. the output should be Ash and Bob since the places visited by Ash also visited by Bob.
Output:
Name1 Name2
Ash Bob
What is a query for this using MySQL or even relational algebra?
The simplest method is to use group_concat(). Assuming no duplicates,
select places, group_concat(names) as names
from (select name, group_concat(place order by place) as places
from t
group by name
) t
group by places
having count(*) > 1;
This will return all the names with exactly the same places on a single row. The names will be in a comma-delimited list.

Count number of times value appears in column in MySQL

Tried looking everywhere for this but my searching hasn't given me any answers.
Looking to count how many times the word 'Sydney' comes up in the column 'Suburb'
CustomerId FirstName LastName StreetAddress Suburb PostCode
C001 James Smith 400 Kent St Sydney 2000
C002 Maria Carpenter 333 Smith St Westmead 2145
C003 Dennis Miller 214 Uni Rd Sydney 2000
I want it to provide an outcome like:
numCustomersFromSydney
2
Use this simple query:
select count(*) as numCustomersFromSydney from table where Suburb = "Sydney";
Use COUNT() and an alias name:
SELECT
COUNT(Suburb) AS numCustomersFromSydney
FROM
yourtable
WHERE
Suburb = 'Sydney'

How do I group by first name in MySQL?

I have a table where there is a full name column. I want to get either the unique given name or given names sorted by how many times they occur.
fullname
-----------
Barack Hussein Obama
Michael Jackson
William Jefferson Blythe
Michael Bloomberg
so the output will be either
Barack Hussein Obama
William Jefferson Blythe
Or
Barack Hussein Obama
William Jefferson Blythe
Michael Jackson
Michael Bloomberg
Or
1|Barack
1|William
2|Michael
Something like that. My aim is to see the foreign students in my database. I only have their full name to make a guess.
You can use SUBSTRING_INDEX(fullname,' ',1) to extract the "given name" as per your definition.
You can then use this for grouping or sorting as you seem fit, e.g.
SELECT COUNT(*),SUBSTRING_INDEX(fullname,' ',1) AS givenname
FROM yourtable
GROUP BY givenname;