Open In App

SQL Query to get information of employee where employee Is Not Assigned to the Department

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss the overview of SQL query and our main focus will be on how to get information of employee where employee Is Not Assigned to the Department in SQL. Let’s discuss it step by step.

Introduction :
Queries help us to interact with the database for various operations of data retrieval, updating, deletion, and inserting. In this article let us see a query to get the information of an employee where the employee is not assigned to any department. When in a table if any attribute is not assigned with any value it would be the NULL so let us execute the query on a table in the database company.

Step-1: Creating a database –
Creating a database company by using the following SQL query as follows.

CREATE DATABASE company;

Output :

Step-2: Using the database –
Using the database company using the following SQL query as follows.

USE company;

Output :

Step-3: Creating a table –
Creating a table employee with 5 columns using the following SQL query as follows.

CREATE TABLE employee
(
emp_id varchar(20),
emp_name varchar(20),
emp_dept varchar(20),
emp_age INT,
emp_sex varchar(8)
);

Output :

Step-4: Verifying the database –
To view the description of the database using the following SQL query as follows.

DESCRIBE employee;

Output :


Step-5: Inserting data into the table –
Inserting rows into employee table using the following SQL query as follows.

 INSERT INTO employee VALUES('E00001','JHONNY','BACKEND DEVELOPER',26,'male');
 INSERT INTO employee VALUES('E00002','DARSHI',NULL,27,'male');
 INSERT INTO employee VALUES('E00003','JASMINE',NULL,37,'female');
 INSERT INTO employee VALUES('E00004','LILLY',NULL,47,'female');
 INSERT INTO employee VALUES('E00005','RONALD','UI DEVELOPER',26,'male'); 

Output :

Step-6: Verifying the inserted data –
Viewing the table employee after inserting rows by using the following SQL query as follows.

SELECT* FROM employee;

Output :

Query to find the employees whose departments are not assigned :
Here, we will see how to query to find the employees whose departments are not assigned by using the following SQL query as follows.

Syntax :

SELECT*
FROM table_name
WHERE column_name  IS NULL;

Selecting Data Query –

 SELECT*
 FROM employee
 WHERE emp_dept IS NULL;

Output :
In this table, all the employee records whose department is NULL value are obtained. 

Query to find the employees whose departments are assigned :
Here, we will see how to query to find the employees whose departments are assigned using the following SQL as follows.

Syntax :

SELECT *
FROM table_name
WHERE column_name IS NOT NULL;

Selecting Query –

SELECT *
FROM employee
WHERE emp_dept IS NOT NULL;

Output :
All the records of an employee whose department is assigned are obtained.

   


Last Updated : 01 Apr, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads