sivakumar V
sivakumar V

Reputation: 135

How to repeat the same SQL query for different column values

I want to execute a same SQL query with different column values, i.e: I have a table like below:

+--------+-------+---------------+
| ENAME  | EID   | BASIC Salary  |
+--------+-------+---------------+
| AAA    | 111   | 12345         |
| BBB    | 222   | 45678         |
| CCC    | 333   | 8965          |
| DDD    | 444   | 3654          |
| .............................. |
| ZZZ    | 555   | 12345         |
+--------+-------+---------------+

From the above table, I want to fetch the EID and Salary for group of employees. Please suggest me how to make a query for getting above data without executing multiple time.

Upvotes: 1

Views: 2040

Answers (2)

Purushotham
Purushotham

Reputation: 3820

Try this query

SELECT ENAME, EID, Salary FROM <TABLENAME> WHERE ENAME IN ('AAA','DDD','ZZZ');

or

SELECT ENAME, EID, Salary FROM <TABLENAME1> WHERE ENAME IN (SELECT ENAME FROM <TABLENAME2> WHERE <CRITERIA>);

Upvotes: 2

Vikram Jain
Vikram Jain

Reputation: 5588

Basic syntex is :

select columnname1, sum(columnname2) from tablename where condition.. group by columnname1

Here, you will be write a query look like :

 select EID, sum(salary) from table where EID in(101,102,103,104,...)
 group by EID 

Here, If you want a 200 or 300 row of data then use skip and limit in sql. You will be write a query look like :

select EID, sum(salary) from table 
     group by EID limit 10 offset 0

where limit is a no of record and offset is a skip a record

Upvotes: 0

Related Questions