Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

SQL Query to Return Rows Matching a Certain Condition

  • Difficulty Level : Expert
  • Last Updated : 14 Nov, 2021

In SQL, sometimes we need to select matching a certain condition from the table. We will use the SELECT command along with the WHERE clause to apply a condition to achieve this in SQL. For this article. We will be using the Microsoft SQL Server as our database.

Syntax:

SELECT COLUMN_NAME_1, COLUMN_NAME_2, ....
 FROM TABLE_NAME WHERE CONDITION;

Step 1: Create a Database. For this use the below command to create a database named GeeksForGeeks.

Query:

CREATE DATABASE GeeksForGeeks

Output:



Step 2: Use the GeeksForGeeks database. For this use the below command.

Query:

USE GeeksForGeeks

Output:

Step 3: Create a table EMPLOYEE inside the database GeeksForGeeks. This table has 6 columns namely E_ID, E_NAME,  E_SALARY, E_AGE, E_GENDER, and E_DEPT containing id, name, salary, age, gender, and department of various employees.

Query:

CREATE TABLE EMPLOYEE(
E_ID INT,
E_NAME VARCHAR(6),
E_SALARY INT,
E_AGE INT,
E_GENDER VARCHAR(6),
E_DEPT VARCHAR(5));

Output:



Step 4: Display the structure of the EMPLOYEE table.

Query:

EXEC SP_COLUMNS 'EMPLOYEE';

Output:

Step 5: Insert six rows into the EMPLOYEE table.

Query:

INSERT INTO EMPLOYEE VALUES(1,'KHUSHI',10000,17,'FEMALE','CSE');
INSERT INTO EMPLOYEE VALUES(2,'KAMLA',20000,37,'FEMALE','EE');
INSERT INTO EMPLOYEE VALUES(3,'GAUTAM',25000,27,'MALE','ME');
INSERT INTO EMPLOYEE VALUES(4,'VIJAY',75000,42,'MALE','CIVIL');
INSERT INTO EMPLOYEE VALUES(5,'ANITA',50000,32,'FEMALE','CSE');
INSERT INTO EMPLOYEE VALUES(6,'MAYURI',42000,49,'FEMALE','CIVIL');

Output:

Step 6: Display all the rows of the EMPLOYEE table.



Query:

SELECT * FROM EMPLOYEE;

Output:

Step 7: If we want to display all the details of the employees who are older than 20 years but younger than 40 years from the EMPLOYEE table. Use the BETWEEN clause to compare the age attribute of the employees.

Syntax of BETWEEN:

ATTRIBUTE LOWER_LIMIT BETWEEN UPPER_LIMIT;

Query:

SELECT * FROM EMPLOYEE WHERE E_AGE BETWEEN 20 AND 40;

Output:

Step 8: Display all the details of the employees who are female from the EMPLOYEE table. Use the =(equal to), relational operator, to compare the gender attribute of the employees to FEMALE.

Query:

SELECT * FROM EMPLOYEE WHERE E_GENDER='FEMALE';

Output:

Step 9: Display all the details of the employees who have a salary greater than 40000 and belong to the civil department from the EMPLOYEE table. We will use >(greater than) relational operator for comparing the salary and =(equal to) relational operator to check the department. We will also use the AND(logical operator) so that both the conditions hold true simultaneously.

Query:

SELECT * FROM EMPLOYEE WHERE E_SALARY>40000 AND E_DEPT='CIVIL';

Output:

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!