Sql Variables & 2nd Highest Salary Interview Question | Mysql, MariaDB & Postgres

Master in Code
12 Sept 202409:04

Summary

TLDRIn this educational video, Bhesh from Master in Code teaches viewers about SQL variables, focusing on their practical applications. He explains how to declare and use variables within SQL queries, demonstrating with examples. The video dives into a real-world scenario, showing how to determine the rank of salaries, including finding employees with the second-highest salary. Bhesh walks through the logic and SQL implementation, step by step, enhancing viewers' understanding of SQL variables and their utility in database management.

Takeaways

  • πŸ˜€ The video is a tutorial on using variables in SQL, focusing on their application in ranking employees by salary.
  • πŸ”’ The video demonstrates how to declare and use variables directly in SQL queries using the colon (:) notation.
  • πŸ“Š It explains the practical use of variables for determining the rank of employees based on their salaries, including handling ties.
  • πŸ’Ό The presenter introduces a real-world example involving a table of employee salaries to illustrate the concept of ranking.
  • πŸ‘¨β€πŸ’» The tutorial walks through the process of implementing a ranking system using SQL variables step by step.
  • πŸ“ The video script includes a live coding session where the presenter writes and explains SQL queries to calculate ranks.
  • πŸ› οΈ It highlights the importance of setting initial values for variables and updating them conditionally based on the data.
  • πŸ”‘ The video addresses a common interview question about finding the second-highest salary and extends it to include all employees with that salary.
  • πŸ” The presenter uses a nested query to filter and display only the employees who have the second-highest salary.
  • πŸŽ“ The video concludes with a summary of the key learnings and a thank you note to the viewers.

Q & A

  • What is the main topic of the video?

    -The main topic of the video is learning about variables in SQL, specifically focusing on how to use them to find the second-highest salary and rank employees accordingly.

  • What are the three main points covered in the video?

    -The three main points covered in the video are: 1) Where to use variables in SQL, 2) How to use variables in SQL, and 3) An interview question on finding the second-highest salary using variables.

  • How does the video demonstrate the use of variables to rank employees by salary?

    -The video demonstrates the use of variables by assigning ranks to employees based on their salaries. It uses a variable to track the previous salary and another to assign a rank, incrementing the rank when a new unique salary is encountered.

  • What is the purpose of the 'previous salary' variable in the SQL example?

    -The 'previous salary' variable is used to store the salary of the previously processed row. It helps in determining whether the current salary is the same as the previous one, which is crucial for assigning correct ranks.

  • How does the video explain the concept of declaring variables in SQL?

    -The video explains that variables can be declared directly in SQL using the 'SET' keyword followed by the variable name and the value it should hold, or within a SELECT query using a colon to denote declaration and an equal sign for assignment.

  • What is the initial value set for the 'rank' variable in the SQL example?

    -The initial value set for the 'rank' variable in the SQL example is zero.

  • What is the initial value set for the 'previous salary' variable in the SQL example?

    -The initial value set for the 'previous salary' variable in the SQL example is 'null'.

  • How does the video handle the case where multiple employees have the same salary?

    -The video handles the case where multiple employees have the same salary by assigning them the same rank and only incrementing the rank when a new, unique salary is encountered.

  • What is the final output the video aims to achieve using the variables?

    -The final output the video aims to achieve is a list of employees with their names, departments, salaries, and ranks, ordered by salary in descending order.

  • How does the video suggest filtering the result to only show employees with the second-highest salary?

    -The video suggests using a nested query and a WHERE clause to filter the results, selecting only those rows where the rank is equal to two, which corresponds to the second-highest salary.

  • What is the significance of using variables in SQL for solving interview questions like this?

    -Using variables in SQL for solving interview questions like finding the second-highest salary is significant because it allows for dynamic calculations and conditional logic that can be used to rank or order data based on specific criteria.

Outlines

00:00

πŸ’» Introduction to SQL Variables and Application

In this segment, the host, Bhesh, welcomes viewers to the 'Master in Code' channel and introduces the topic of SQL variables. The video aims to teach viewers where and how to use SQL variables, with a focus on a practical example: finding the second-highest salary among employees. The host explains that the goal is not just to find the second-highest salary but to identify all employees who share this salary. The video also touches on how to use the 'WHERE' clause in conjunction with variables to print out employee details and salaries. Bhesh demonstrates how to declare variables in SQL, either directly by setting a value or within a SELECT query. The host then dives into the terminal to show a practical demonstration of these concepts using MySQL installed on Docker Desktop.

05:00

πŸ”’ Implementing SQL Variable Logic for Salary Ranking

The second paragraph delves into the practical application of SQL variables to solve an interview question: determining the rank of employees based on their salaries. The host outlines the logic for this task, which involves comparing the current salary with a 'previous salary' variable to determine rank. The initial values for 'rank' and 'previous salary' are set, and the logic is implemented to increment the rank when the current and previous salaries differ. The host then attempts to implement this logic in SQL, facing a minor issue with the syntax but quickly correcting it. The video demonstrates how to use variables to calculate and display ranks alongside employee details, such as name and department. The host concludes by showing how to filter the results to find employees with the second-highest salary, emphasizing the utility of SQL variables in solving complex queries.

Mindmap

Keywords

πŸ’‘Variables

In the context of the video, 'variables' refer to the storage locations paired with symbolic names, which are used to store values in SQL. Variables are essential for programming and data manipulation, allowing users to store intermediate results or specific values for use in queries. The video explains how to declare and use variables in SQL to calculate ranks of salaries, demonstrating the concept with a practical example where variables are used to determine the second-highest salary among employees.

πŸ’‘SQL

SQL, or Structured Query Language, is a domain-specific language used in programming and designed for managing data held in a relational database management system. The video focuses on teaching viewers how to use SQL variables to perform operations such as finding the second-highest salary. SQL is central to the video's theme as it is the tool used to manipulate and retrieve data from databases.

πŸ’‘Ranking

Ranking in the video refers to the process of assigning a unique rank to each row in a result set based on a specified order. The video demonstrates how to use SQL variables to rank employees by their salaries, which is a common operation in database queries when dealing with ordered data sets. The concept is illustrated through an example where employees are ranked according to their salary, and the focus is on identifying those with the second-highest salary.

πŸ’‘Salary

Salary is a key term in the video as it represents the compensation paid to employees. The tutorial aims to find out the second-highest salary using SQL variables. It serves as the basis for the ranking system explained in the video, where the salaries of employees are compared to determine their rank within the company's payroll structure.

πŸ’‘Docker Desktop

Docker Desktop is a development environment that allows users to build, debug, and share applications. In the video, the presenter mentions using Docker Desktop to install MySQL, which is a relational database management system. Docker Desktop is used here to provide a platform for running the SQL commands and demonstrating the concepts discussed in the tutorial.

πŸ’‘MySQL

MySQL is an open-source relational database management system. The video script mentions logging into the MySQL terminal installed in Docker Desktop to demonstrate SQL queries. MySQL serves as the practical platform where the concepts of variables and ranking are applied to solve the interview question about finding the second-highest salary.

πŸ’‘Interview Question

The video addresses a common interview question related to SQL, which is finding the second-highest salary among employees. This question tests the candidate's ability to manipulate data using SQL queries. The video provides a step-by-step approach to solving this problem, showcasing the practical application of SQL knowledge in a job interview scenario.

πŸ’‘SELECT Query

The SELECT query is a fundamental operation in SQL used to retrieve data from a database. In the video, the presenter uses SELECT queries to declare variables and to perform operations like finding the second-highest salary. The SELECT query is integral to the video's demonstration as it is the primary method for interacting with the database and extracting the required information.

πŸ’‘RANK

RANK in the video is a concept used to assign a unique sequence number to each row within a partition of a result set, based on the order by a specified column. The video explains how to calculate the rank of each employee's salary to identify those with the second-highest salary. The RANK concept is crucial for understanding how to order and partition data in SQL.

πŸ’‘Nested Query

A nested query, also known as a subquery, is a query that is nested within another query. The video mentions using a nested query to filter out employees who have the second-highest salary. This technique is a common approach in SQL for performing complex data retrieval operations, and in the context of the video, it is used to solve the interview question effectively.

πŸ’‘ORDER BY Clause

The ORDER BY clause in SQL is used to sort the result set in ascending or descending order. In the video, the presenter uses the ORDER BY clause to sort employees by their salaries in descending order, which is necessary for ranking and identifying the second-highest salary. This clause is a key component in the SQL queries demonstrated in the tutorial.

Highlights

Introduction to learning variables in SQL

Content outline: use cases for variables, how to use them, and an interview question on finding the second-highest salary

Explanation of ranking employees by salary, including handling ties

Demonstration of using the WHERE clause to filter results based on variables

Step-by-step guide to declaring a variable directly in SQL

Example of assigning a value to a variable using the SET statement

How to declare and use a variable within a SELECT query

Introduction to the interview question: finding all employees with the second-highest salary

Logic behind using variables to determine salary ranks

Initial setup of variables 'Rank' and 'Previous Salary' with null and zero values

Incrementing the rank based on the difference between previous and current salary

Implementation of logic in SQL to assign ranks to employees

Using variables to print employee details with their salary ranks

Fixing an issue with NULL values in the variable assignment

Final SQL query to display employee names, departments, salaries, and ranks

Filtering to find only employees with the second-highest salary rank

Conclusion and summary of the video's learnings

Transcripts

play00:00

hello everyone my name is bhesh welcome

play00:02

back to my Channel Master in code I hope

play00:05

you are doing well today we are going to

play00:07

learn variables in SQL so content of

play00:11

this videos will be where do we use this

play00:14

variables then second how can we use it

play00:18

and at last we will cover one interview

play00:20

question that is finding out the second

play00:22

highest salary now this time we will not

play00:24

find out only the second highest salary

play00:26

but we will find out all those employees

play00:28

who has second highest salary

play00:30

and where can we use this now let me

play00:33

show you one example

play00:36

okay so as you can see here I have one

play00:39

table Sam has the highest salary then

play00:42

Alex has the second highest salary but

play00:45

if you see Mike also has the second

play00:47

highest salary so his rank also should

play00:49

be two right then Lisa's rank should be

play00:53

three then Bob's rank should be four and

play00:56

if you see Jacob also has same salary as

play00:59

Bob so his rank also should be four okay

play01:04

and for Mary it should be five now once

play01:08

we get this ranking then we can use the

play01:11

wear Clause to print out the Y salary

play01:14

okay along with the employee details so

play01:18

that's something new when we are using

play01:20

the variables okay now let's see how we

play01:23

can use the variables in SQL let's go to

play01:27

the terminal so this is my MySQL

play01:29

terminal

play01:30

I have installed in the docker desktop

play01:32

so let's login in

play01:37

that and

play01:39

password

play01:41

okay show databases will show me the all

play01:46

databases then we will say use

play01:51

study so now I in the study databas if I

play01:55

say show tables it will show me list of

play01:57

all the tables right

play02:01

now how to declare the variable okay

play02:05

first method is directly you use set the

play02:09

variable name War one equal to some

play02:13

value consider

play02:16

Master right now if I want to print I

play02:19

will just simply say

play02:22

select

play02:24

forward okay and when I was discussing

play02:28

this to find out the ranks same ranks

play02:31

this will be generated using the select

play02:33

query so how do we use in select query

play02:36

let's check that so to declare a direct

play02:40

new variable in select query we can

play02:43

simply say select War

play02:47

to colon equal to 3 okay so here colon

play02:53

is used for declaring the variable and

play02:55

equal to is used for assigning the value

play02:59

right

play03:00

now you can see it has print the three

play03:04

okay

play03:08

now so now I hope you understood how to

play03:11

declare the variables how to use it

play03:14

right

play03:16

now now we will move to our interview

play03:19

question that is finding out the second

play03:21

or highest salary using the variables

play03:24

right so let's discuss the logic first

play03:27

right so here

play03:30

now what I will do I will have two

play03:34

variables one is called Rank and second

play03:37

will be the previous salary here

play03:40

initially I will say previous salary

play03:42

equal to

play03:44

null and rank equal to zero so what my

play03:50

logic will be if previous salary and

play03:52

current salary are different then

play03:56

increase my rank by one okay now for

play04:00

this rank will be the one and assign

play04:04

current salary to the previous salary

play04:06

variable right now here what will

play04:11

happen is this previous salary equal to

play04:14

this salary no then increase the rank

play04:18

okay by one so that becomes two here and

play04:21

it becomes one here and we will assign

play04:24

current salary to the previous salary

play04:27

right now

play04:30

previous salary and current salary are

play04:32

same so what happens increase the rank

play04:36

by zero or don't increase what will

play04:40

happen rank is two okay right so Alex

play04:44

and Mike has second highest salary

play04:46

that's true and salary equal to previous

play04:50

highest salary and so on so this logic

play04:54

we will try to implement in our SQL

play04:56

right now

play05:00

uh let's try to implement our

play05:03

logic what we want to print

play05:05

name Department

play05:09

salary

play05:11

and order by

play05:14

salary descending right let's go yeah

play05:19

now let's declare those both variables

play05:23

how we

play05:24

declared select add direct War equal to

play05:28

3 Let's copy

play05:30

for Simplicity and then our another

play05:34

variable so we don't have to put select

play05:36

again we'll remove it first variable

play05:38

will be

play05:40

rank another variable will be previous

play05:45

salary

play05:47

right and let's try to print here Rank

play05:53

and previous

play05:57

salary alas every der table Yeah so this

play06:01

is our der table and it should be having

play06:04

the alas so we'll say

play06:07

Wars

play06:09

okay right so what we decided uh initial

play06:13

values for rank we'll say zero and for

play06:17

previous salary we will say null okay

play06:20

zero and null right and then we

play06:26

said every time for each row we will

play06:31

change

play06:32

salary previous salary to current salary

play06:36

okay and we'll say here alas as previous

play06:42

sell

play06:47

okay now it is saying

play06:52

null maybe this was the issue yeah this

play06:56

was the issue excellent now let's work

play06:59

on the rank okay so rank

play07:04

as rank one and we'll say rank equal to

play07:10

rank +

play07:12

1

play07:16

right so it also need the

play07:20

colon so whenever we are updating the

play07:22

value they are asking for to use colon

play07:24

right so now it is increasing by plus

play07:27

one but what was our logic

play07:30

increase by zero if previous salary this

play07:34

one previous salary and current salary

play07:35

are same so how can we do

play07:39

that let's instead of one we will say

play07:46

if previous

play07:49

salary equal

play07:51

to current salary then increase by zero

play07:56

else increase by 1

play08:00

okay something is wrong

play08:05

here yeah I missed one closing

play08:09

bracket yes now for Sam rank is one for

play08:14

Alex rank is two for my rank is

play08:18

two and if you want to see employee who

play08:21

has second highest salary only in that

play08:24

case just do

play08:26

select star from

play08:32

add one nested query uh on the top of

play08:36

this and

play08:38

where rank

play08:40

one equal to two right yeah so yes you

play08:45

got the second highest salary yeah so

play08:49

this is how we can use the variables to

play08:50

calculate the rank in the table so yes

play08:53

uh that's all about this video this was

play08:55

my third and last video of calculating

play08:57

the second highest salary I hope you

play09:00

have learned something new today thank

play09:01

you

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
SQL TutorialCoding SkillsDatabase ManagementSalary RankingMySQLVariable UsageProgrammingData AnalysisEducational ContentTech Tutorial