Cassandra, find details of second largest salary - database

**I want to get the seocond largest salary **
select Max(Salary) from table;
Is there any word like skip in cassandra??

Related

A table contains name and amount field. Amount can be positive or negative. Query to delete names for whom total amount is 0

A table contains name and amount field. Amount can be positive or negative. Amount is positive if debited and negative if credited. Query to delete names for whom total amount is 0.
SELECT name , sum(amount) as total FROM table_name group by name having total =0;
same way Delete query would be:
Delete FROM table_name group by name having sum(amount) =0;

How to select the second max value from a table by comparing the values among itself

I am working on a project and I need to select maximum value of "Balance" field for each account. But if we have one more rows with opposite sign, we should remove this pair of 2 rows from the search and select the 2nd highest value. Here is an example:
Tablet1 (Account varchar(10), Balance money)
A1, 100
A1, 50
A1, -100
A1, 30
So the search should return 50 because we have 100 and -100.
One way is to use EXCEPT to remove the opposite pairs then work the usual way with the reamining data.
SELECT account, MAX(balance)
FROM (SELECT account, balance
FROM table1
WHERE balance > 0
EXCEPT
SELECT account, -balance
FROM table1
WHERE balance < 0) data
GROUP BY account
SQLFiddle demo
EXCEPT work the opposite way of UNION, removing from the first dataset all the corresponding data in the second dataset.

Sql Server multiple word search and ranking the results

Assume user enters "Vintage Audi Car" as search criteria
My table has a field "Description" as Varchar(Max)
I want to search the user entered values as Vintage OR Audi OR Car. The result should display the rows that contains all the three words in "Description" at top and rows with any two words and then rows with any one word.
Let me know how this can be achieved.
I am open to use Full Text Search.
That should be possible using full text search with the command CONTAINSTABLE:
SELECT * FROM Car
INNER JOIN CONTAINSTABLE(Car, Description, 'ISABOUT (Vintage weight (.5), Audi weight (0.5), Car weight (0.5) )') AS A
ON Car.Id = A.[KEY]
ORDER BY A.[RANK] DESC;
The rank is calculated by the weight, so a row with the description "Vintage Audi" will get a higher rank than the row with the description "Audi".
Here is one way of achieving this result using the LIKE operator:
select Description
from MyTable
where Description like '%Vintage%' OR Description like '%Audi%' OR Description like '%Car%'
order by (case when Description like '%Vintage%' then 1 else 0 end
+ case when Description like '%Audi%' then 1 else 0 end
+ case when Description like '%Car%' then 1 else 0 end) DESC
The WHERE clause limits the result to descriptions that contain at least one of the words (hence the OR). The ORDER BY adds 1 for each word from the list, bringing items with higher score to the top.
Even i had same issue i solved it using MYISAM engine
alter the table Car
ALTER TABLE Car ENGINE = MYISAM;
then
select * FROM Car WHERE MATCH (Description) AGAINST ('Vintage Audi Car' IN BOOLEAN MODE);

Need help understanding an SQL query

I have a table in SQL, Employee_Details, having three columns, Name, Age and Salary. I came across this query to select the highest salary from the table which goes like this.
SELECT *
FROM Employee_Details e
WHERE 0 = (
SELECT count(DISTINCT Salary)
FROM Employee_Details
WHERE Salary > e.Salary
)
I have no idea about what the '0' signifies. Could anyone please give me an idea.
It's just a condition that says that the count from the Employee_Details table in the inner SELECT query must be 0 (no one can have a higher salary than the Employee selected in the outer SELECT)
The approach to do it this way seems a bit odd to me..... I would probably have used something like this:
SELECT (columns)
FROM dbo.Employee_Details e
WHERE e.Salary = (SELECT MAX(Salary) FROM dbo.Employee_Details)
which should produce the same result - only it seems a lot clearer to me what you're trying to achieve.
Get all from table Employee_details where count of salary is 0
Your query could be written as SELECT TOP 1 WITH TIES * FROM Employee_Details e ORDER BY Salary DESC (in T-SQL dialect). Condition in original query checks if the number of distinct values of Salary greater than Salary value in e is zero (i.e. there are no rows with greater Salary).

Can somebody explain how the following SQL query to get the 'n' highest salary works

SELECT *
FROM employee A
WHERE 3=(select count(*) +1
from employee B
where B.salary > A.salary)
This gets the 3rd highest salary; can somebody explain the logic behind this query and how it works.
In words, this query would be "Select the employee who has two other people with a greater salary." So, the result is the employee with the third highest salary.
Note that this query can fail if there are two or more people with the exact same salary.
This will only work with Distinct Salaries:
For every employee count the number of rows where salary is greater then employee salary. If the count is 2 + 1, return the employee
Therefore it will return the 3rd emplyee.
I would do this with SELECT TOP 1 FROM (SELECT TOP 3 * FROM Employee ORDER BY Salary DESC) a ORDER BY SALARY ASC
This is what is known as a correlated subquery. You can think of it as looping over all the records in the outer query and for each one it evaluates the query in the where clause. (This happens because the query in the where clause references the alias "A" of the outer query)
So for each employee in gets the count of the number of employees with a higher salary.
You could probably implement this logic faster in SQL 2005 & 2008 by using the ROW_NUMBER function.
eg.
WITH SalaryOrder AS
(
SELECT *
, ROW_NUMBER() OVER(ORDER BY Salary DESC) SalaryRank
FROM employee
)
SELECT *
FROM SalaryOrder
WHERE SalaryRank = 3
Just to illustrate this with an example, say the salary are as below; repeated the data for the B,
EmpA EmpB
5000 5000
3000 3000
2000 2000
1500 1500
1000 1000
500 500
In the first parse, A.Salary is 5000, so all the Count of salary from B which exceeds 5000 is 0. Add one and its 1. Now this will be the highest salary.
In your example, A.Salary is 2000, so all the count of salary from B which exceeds 2000 will be 2, add one and it will be 3. Join 3=3 and A.Salary with value 2000 will get selected.

Resources