Finding maximum from column of table - mysql

I want to find maximum value column in given table in MySql.
Table has following entry :
A | B | C | D
10 | 50 | 60 | 20|
Where A,B,C,D are column names and 10,50,60,20 are data points.
Expected output : C

select greatest(a,b,c,d)
from your_table

You can use if condition in select query.I will show the example for 3 columns in a table.
if condition syntax is as follows
if(condition,return value if true,return value if false)
This is the query
select if(col1>col2,if(col1>col3,col1,col3),if(col2>col3,col2,col3)) from your_table;

Related

Is it possible to get sum without change sql_mode?

I have a problem to get SUM value from multiple table using join statement. The error is:
this is incompatible with sql_mode=only_full_group_by
Is it possible to get sum without change sql_mode? If possible, how to make a SQL statement to?
Table fuel:
vehicle_id | liter
-----------+-----------
2 | 43.5
4 | 78.3
8 | 20.5
Table usage:
date_usage | vehicle_id
-----------+-----------
2019-10-01 | 8
2019-10-15 | 2
2019-10-20 | 8
2019-10-20 | 4
2019-11-02 | 8
The SQL statement is below:
SELECT fuel.vehicle_id, SUM(fuel.liter), usage.date_usage
FROM fuel
LEFT JOIN usage ON fuel.vehicle_id = usage.vehicle_id
WHERE fuel.vehicle_id='8'
AND usage.date_usage >='2019-10-01' AND usage.date_usage <='2019-10-31'
GROUP BY fuel.vehicle_id
You have two possibilities to solve this:
Remove column usage.date_usage from SELECT.
Use a aggregate function on column usage.date_usage too:
MAX to get the highest value of the column.
MIN to get the lowest value of the column.
ANY_VALUE to get any value of the column.
So your query can look like the following (using MAX on column usage.date_usage):
SELECT fuel.vehicle_id, SUM(fuel.liter), MAX(`usage`.date_usage)
FROM fuel LEFT JOIN `usage` ON fuel.vehicle_id = `usage`.vehicle_id
WHERE fuel.vehicle_id = 8
AND `usage`.date_usage BETWEEN '2019-10-01' AND '2019-10-31'
GROUP BY fuel.vehicle_id
demo on dbfiddle.uk
Note: be careful using words like usage as identifiers (e.g. table and column names) in MySQL. There are keywords and reserved words you should avoid or quote with backticks.

add two table per row based on its name

I have two tables and i want to add there row based on its name. I've search on net but I only found how to combine the total value or two tables and combine them. The result will be added on a table named Result
Table 1 Table 2 Result
Name | Value Name | Value Name | Value
Apple | 2 Apple | 4 Apple | 6
Orange | 3 Orange | 2 Orange| 5
Thank you in advance
First of all I would like to say you must try to get solution at your own.
For your case, answer is very simple. Try this query :
SELECT table1.`name`, (table2.value + table1.value) AS `value` FROM table1
LEFT JOIN table2 ON table1.`name` = table2.`name` WHERE table1.`name` = table2.`name`

Generic average over multiple entries in a sql column

I have a SQL table that looks something like this:
| variable | rank |
|__________|______|
| var1 | 0.3 |
| var2 | 0.1 |
| ... | ... |
I have hundreds of entries in the table with multiple instances of every variable with some associated rank. Is there a way I can tell SQL to take the entries for every variable in the first column and output me an average ranking? What I don't want to do is this:
SELECT AVG(column_name)
FROM table_name
WHERE variable=variable_name;
where I would need to specify every single variable.
Yes, it is called GROUP BY:
SELECT variable,AVG(column_name)
FROM table_name
--WHERE variable IN (...) -- you still can filter rows based on variable values
GROUP BY variable

Mysql-Select all tables from a database

I've a database called test and i've tables called x,y,z.
How do i select x,y,z and there is a column called date IN X,Y,Z check whether there is a particular date.
Is there any build in function that does this?
update
SELECT column date from all tables which is in a database called test
Thanks in advance!!
As far as I know, in SQL you cannot 'select a table', you can select some
column(s) from one or many tables at once. The result of such a query is an another table (temporary table) that you retrieve the data from.
Please be more specific about what exactly you want to do (e.g.: "I want to select a column 'z' from table 'tableA' and column 'y' from table 'tableB'") - then I'm sure your question has a pretty simple answer :)
SELECT x.date AS x_date, y.date AS y_date, z.date AS z_date FROM x,y,z;
That produces a result:
+---------+---------+---------+
| x_date | y_date | z_date |
+---------+---------+---------+
| | | |
| | | |
+---------+---------+---------+
Alternatively you can get everything in one column by ussuing a query:
SELECT date FROM x
UNION ALL
SELECT date FROM y
UNION ALL
SELECT date FROM z;
That produces a result:
+-------+
| date |
+-------+
| |
| |
+-------+
In the example above you would get also duplicate values in the single column. If you want to avoid duplicates replace 'UNION ALL' with 'UNION'
I'm still not sure if I undestood what you really want ot achieve, but I still hope that helps
Also take a look at:
http://www.w3schools.com/sql/sql_union.asp
http://www.sql-tutorial.net/SQL-JOIN.asp

How does SELECT DISTINCT work in MySQL?

I have a table with multiple rows which have a same data. I used SELECT DISTINCT to get a unique row and it works fine. But when i use ORDER BY with SELECT DISTINCT it gives me unsorted data.
Can anyone tell me how distinct works?
Based on what criteria it selects the row?
From your comment earlier, the query you are trying to run is
Select distinct id from table where id2 =12312 order by time desc.
As I expected, here is your problem. Your select column and order by column are different. Your output rows are ordered by time, but that order doesn't necessarily need to preserved in the id column. Here is an example.
id | id2 | time
-------------------
1 | 12312 | 34
2 | 12312 | 12
3 | 12312 | 48
If you run
SELECT * FROM table WHERE id2=12312 ORDER BY time DESC
you will get the following result
id | id2 | time
-------------------
2 | 12312 | 12
1 | 12312 | 34
3 | 12312 | 48
Now if you select only the id column from this, you will get
id
--
2
1
3
This is why your results are not sorted.
When you specify SELECT DISTINCT it will give you all the rows, eliminating duplicates from the result set. By "duplicates" I mean rows where all fields have the same values. For example, say you have a table that looks like:
id | num
--------------
1 | 1
2 | 3
3 | 3
SELECT DISTINCT * would return all rows above, whereas SELECT DISTINCT num would return two rows:
num
-----
1
3
Note that which row actual row (eg: whether it's row 2 or row 3) it selects is irrelevant, as the result would be indistinguishable.
Finally, DISTINCT should not affect how ORDER BY works.
Reference: MySQL SELECT statement
The behaviour you describe happens when you ORDER BY an expression that is not present in the SELECT clause. The SQL standard does not allow such a query but MySQL is less strict and allows it.
Let's try an example:
SELECT DISTINCT colum1, column2
FROM table1
WHERE ...
ORDER BY column3
Let's say the content of the table table1 is:
id | column1 | column2 | column3
----+---------+---------+---------
1 | A | B | 1
2 | A | B | 5
3 | X | Y | 3
Without the ORDER BY clause, the above query returns following two records (without ORDER BY the order is not guaranteed):
column1 | column2
---------+---------
A | B
X | Y
But with ORDER BY column3 the order is also not guaranteed.
The DISTINCT clause operates on the values of the expressions present in the SELECT clause. If row #1 is processed first then (A, B) is placed in the result set and it is associated with row #1. Then, when row #2 is processed, the values of the SELECT expressions produce the record (A, B) that is already in the result set. Because of DISTINCT it is dropped. Row #3 produces (X, Y) that is also put in the result set. Then, the ORDER BY column3 clause makes the records be sorted in the result set as (A, B), (X, Y).
But if row #2 is processed before row #1 then, following the same logic exposed in the previous paragraph, the records in the result set are sorted as (X, Y), (A, B).
There is no rule imposed on the database engine about the order it processes the rows when it runs a query. The database is free to process the rows in any order it consider it's better for performance.
Your query is invalid SQL and the fact that it can return different results using the same input data proves it.