SQL Tutorial 22: Find Employees with 5+ Years of Experience

SQL Tutorial 22: Find Employees with 5+ Years of Experience

126 Lượt nghe
SQL Tutorial 22: Find Employees with 5+ Years of Experience
Watch the entire SQL playlist here: https://youtube.com/playlist?list=PL_neLGJ5LPUzPIg96tf7rXb57D6-3n5Pq&si=15bYAv5Q3iJqwW4Q In this tutorial, we walk through an example of how to calculate the years of experience for employees using SQL. We’ll show how to handle NULL values with the COALESCE function and accurately calculate years of work using DATEDIFF to consider both resigned and currently employed workers. Schema & Sample Data: CREATE TABLE Work_force ( WorkerID INT PRIMARY KEY, FullName VARCHAR(100), JoinDate DATE, ResignDate DATE NULL ); INSERT INTO Work_force (WorkerID, FullName, JoinDate, ResignDate) VALUES (1, 'Amit Sharma', '2015-03-15', NULL), -- Still working (2, 'Priya Iyer', '2017-07-10', '2022-06-30'), -- Resigned before completing 5 years (3, 'Rajesh Verma', '2013-02-20', '2020-01-15'), -- Completed 5+ years before resigning (4, 'Neha Patel', '2016-11-05', NULL), -- Still working (5, 'Vikram Rao', '2018-03-01', '2024-02-29'), -- Just completed 6 years before resigning (6, 'Sneha Menon', '2019-09-10', NULL), -- Less than 5 years, still working (7, 'Karan Mehta', '2021-06-25', NULL), -- Joined recently, still working (8, 'Ritika Gupta', '2020-11-05', '2025-01-30'), -- Resigned after completing more than 5 years (9, 'Rohit Kumar', '2022-01-15', NULL), -- Recently joined, still working (10, 'Divya Singh', '2021-03-12', NULL); -- Recently joined, still working -- Expected Output: -- +----------+------------------+------------+------------+----------------+ -- | WorkerID | FullName | JoinDate | ResignDate | Years_Completed | -- +----------+------------------+------------+------------+----------------+ ---1 Amit Sharma 2015-03-15 NULL 10.0 -- 3 Rajesh Verma 2013-02-20 2020-01-15 6.9 -- 4 Neha Patel 2016-11-05 NULL 8.3 -- 5 Vikram Rao 2018-03-01 2024-02-29 5.9 -- 6 Sneha Menon 2019-09-10 NULL 5.5 -- Syntax: DATEDIFF (datepart, startdate, enddate) -- Syntax: coalesce() Follow me on LinkedIn: https://www.linkedin.com/in/paragtech/ Relevant Hashtags: #SQL #Database #SQLQuery #SQLInterview #LearnSQL #DataScience #SQLChallenge #SQLTutorial #SQLForBeginners #SQLQueries #SQLServer #MySQL #PostgreSQL #SQLPractice #SQLDeveloper #CodingChallenge #TechInterview #DataAnalytics #SQLExercises #Programming #SoftwareDevelopment