Latest Posts

Tuesday, July 18, 2023

How to Find Second Highest Salary in SQL command

KK EDU SOLUTIONS

 

How to Find Second Highest Salary in SQL

 

In this article, we will look up a frequently asked SQL interview question. So the problem statement is that given a table of employees with their names and salary.

The task is to find the second highest salary on that table. For the purpose of understanding, let’s create a table using the below SQL command:

 

-- create a table named employees 

CREATE TABLE employees ( 

  name TEXT NOT NULL, 

  salary INTEGER NOT NULL 

);                                                                                                                                                       

 

 

The above code will create an empty table named employees with two rows namely, name and salary

Now let’s insert some data to the table so that we can make queries to the table using the below SQL commands:

-- insert data into employees table 

INSERT INTO employees VALUES ('Amar', 200000); 

INSERT INTO employees VALUES ('Akbhar', 5000000) 

INSERT INTO employees VALUES ('Anthony', 7000000); 

INSERT INTO employees VALUES ('Mike', 6000000) 

INSERT INTO employees VALUES ('Harvey', 4000000);

 

 

To check out the employees table at this point use the below SQL command:

 

 

 

 

 

SELECT * FROM employees; 

 

 

 

 

 

 

Output:

 



 

 

Clearly, Mike has the 2nd highest salary in this case. But as the data set becomes bigger, merely observing the data and filtering the 2nd highest salary can be hectic. Therefore coming back to the problem statement let’s design an SQL query for finding out the second highest salary in the employees table. 

Now, we will see the steps how to find 2nd highest salary in SQL.

 

SQL Query to Find the Second Highest Salary. 

Below is the list of methods that can be used to find the second highest salary. 

Method 1: Excluding the highest Salary

This method involves making a subquery that excludes the maximum salary from the query dataset and finds the next highest salary in the new data set after exclusion of the highest salary that technically is the second highest salary in the employees table. Use the below SQL query for the same: 

 

                                                         

SELECT MAX(SALARY) FROM employees

WHERE SALARY < (SELECT MAX(SALARY) FROM employees);

 

 



 

Output:

 

 


 

Method 2: Using Correlated SubQuery

This method makes use of correlated subqueries. This solution can also be used to get the Nth highest salary in the employees table.  The basic idea is that for each processed record by the outer query, the inner query gets executed and returns the records with a salary less than that of the current salary. In our case we need the second highest salary, so we will stop our query as soon as the query inside returns the value 2. 

 

 

SELECT salary FROM employees e

WHERE 2 = (SELECT COUNT(DISTINCT salary) FROM employees p WHERE e.salary<=p.salary);

 

 

 



 

Output:

 

 

Method 3: Using LIMIT Clause

This method involves making use of the LIMIT query to get the second highest salary from employees table. The LIMIT clause can be used to query the first few rows, last few rows, or rows within a range. Here we will also be using the SORT BY clause to sort the query set.  So the SQL query goes as follows: 

 

SELECT salary FROM (SELECT salary FROM employees ORDER BY salary DESC LIMIT 2)

AS emp ORDER BY salary LIMIT 1; 

 

 

 

Output:

 



 

 

Conclusion: 

In this article, we explored the following methods to find the second highest salary in a table of employees:

·         Excluding the highest salary from the query set.

·         Using correlated subquery

·         Using the LIMIT clause

Sunday, July 9, 2023

Class-3 Pl Sql

KK EDU SOLUTIONS

 





Notes:-   🠋             jobs updates 👉 click me 


SET OPERATORS:

-------------

1. UNION     -->It display the only uniq records. 

2. UNION ALL -->It display the all the records from both the tables.

3. INTERSECT -->It display the common records from both the tables.

4. MINUS     -->It will minus the common records from first table and display the remaining first

                table records.


DM1

---

CMOB  CNAME

 1      A

 2      B

 3      C

 4      D

 5      E


DM2

---

CMOB  CNAME

 6      F

 7      G

 8      H

 1      A

 3      C



SQL> CREATE TABLE DM1

  2  (CMOB NUMBER(10),

  3  CNAME VARCHAR2(10));


Table created.


SQL> INSERT INTO DM1 VALUES(1, 'A');


1 row created.


SQL> INSERT INTO DM1 VALUES(2, 'B');


1 row created.


SQL> INSERT INTO DM1 VALUES(3, 'C');


1 row created.


SQL> INSERT INTO DM1 VALUES(4, 'D');


1 row created.


SQL> INSERT INTO DM1 VALUES(5, 'E');


1 row created.


SQL> CREATE TABLE DM2

  2  (CMOB NUMBER(10),

  3  CNAME VARCHAR2(10));


Table created.


SQL> INSERT INTO DM2 VALUES(6, 'F');


1 row created.


SQL> INSERT INTO DM2 VALUES(7, 'G');


1 row created.


SQL> INSERT INTO DM2 VALUES(8, 'H');


1 row created.


SQL> INSERT INTO DM2 VALUES(1, 'A');


1 row created.


SQL> INSERT INTO DM2 VALUES(3, 'C');


1 row created.


SQL> SELECT * FROM DM1;


      CMOB CNAME

---------- ----------

         1 A

         2 B

         3 C

         4 D

         5 E


SQL> SELECT * FROM DM2;


      CMOB CNAME

---------- ----------

         6 F

         7 G

         8 H

         1 A

         3 C


SQL> SELECT * FROM DM1

  2  UNION

  3  SELECT * FROM DM2;\

  4

SQL> SELECT * FROM DM1

  2  UNION

  3  SELECT * FROM DM2;


      CMOB CNAME

---------- ----------

         1 A

         2 B

         3 C

         4 D

         5 E

         6 F

         7 G

         8 H


8 rows selected.


SQL> SELECT * FROM DM1

  2  UNION ALL

  3  SELECT * FROM DM2;


      CMOB CNAME

---------- ----------

         1 A

         2 B

         3 C

         4 D

         5 E

         6 F

         7 G

         8 H

         1 A

         3 C


10 rows selected.


SQL> SELECT * FROM DM1

  2  INTERSECT

  3  SELECT * FROM DM2;


      CMOB CNAME

---------- ----------

         1 A

         3 C


SQL> SELECT * FROM DM1

  2  MINUS

  3  SELECT * FROM DM2;


      CMOB CNAME

---------- ----------

         2 B

         4 D

         5 E


SQL> SELECT * FROM DM2

  2  MINUS

  3  SELECT * FROM DM2;


no rows selected


SQL> ED

Wrote file afiedt.buf


  1  SELECT * FROM DM2

  2  MINUS

  3* SELECT * FROM DM1

SQL> /


      CMOB CNAME

---------- ----------

         6 F

         7 G

         8 H


SQL> SELECT * FROM EMP;


     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO

---------- ---------- --------- ---------- --------- ---------- ---------- ----------

      7369 SMITH      CLERK           7902 17-DEC-80        800                    20

      7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300         30

      7521 WARD       SALESMAN        7698 22-FEB-81       1250        500         30

      7566 JONES      MANAGER         7839 02-APR-81       2975                    20

      7654 MARTIN     SALESMAN        7698 28-SEP-81       1250       1400         30

      7698 BLAKE      MANAGER         7839 01-MAY-81       2850                    30

      7782 CLARK      MANAGER         7839 09-JUN-81       2450                    10

      7788 SCOTT      ANALYST         7566 19-APR-87       3000                    20

      7839 KING       PRESIDENT            17-NOV-81       5000                    10

      7844 TURNER     SALESMAN        7698 08-SEP-81       1500          0         30

      7876 ADAMS      CLERK           7788 23-MAY-87       1100                    20

      7900 JAMES      CLERK           7698 03-DEC-81        950                    30

      7902 FORD       ANALYST         7566 03-DEC-81       3000                    20

      7934 MILLER     CLERK           7782 23-JAN-82       1300                    10


14 rows selected.


SQL> SELECT ENAME FROM EMP WHERE DEPTNO=10

  2  UNION ALL

  3  SELECT JOB FROM EMP WHERE DEPTNO=10;


ENAME

----------

CLARK

KING

MILLER

MANAGER

PRESIDENT

CLERK


6 rows selected.


SQL> SELECT ENAME FROM EMP WHERE DEPTNO=10

  2  UNION

  3  SELECT JOB FROM EMP WHERE DEPTNO=10;


ENAME

----------

CLARK

CLERK

KING

MANAGER

MILLER

PRESIDENT


6 rows selected.


SQL> SELECT ENAME FROM EMP WHERE DEPTNO=10

  2  UNION ALL

  3  SELECT JOB FROM EMP WHERE DEPTNO=30;


ENAME

----------

CLARK

KING

MILLER

SALESMAN

SALESMAN

SALESMAN

MANAGER

SALESMAN

CLERK


9 rows selected.


SQL> ED

Wrote file afiedt.buf


  1  SELECT ENAME FROM EMP WHERE DEPTNO=10

  2  UNION

  3* SELECT JOB FROM EMP WHERE DEPTNO=30

SQL> /


ENAME

----------

CLARK

CLERK

KING

MANAGER

MILLER

SALESMAN


6 rows selected.


SQL> SELECT ENAME FROM EMP WHERE DEPTNO=10

  2  UNION ALL

  3  SELECT SAL FROM EMP WHERE DEPTNO=10;

SELECT ENAME FROM EMP WHERE DEPTNO=10

       *

ERROR at line 1:

ORA-01790: expression must have same datatype as corresponding expression



SQL> SELECT ENAME, SAL FROM EMP WHERE DEPTNO=10

  2  UNION ALL

  3  SELECT SAL FROM EMP WHERE DEPTNO=10;

SELECT ENAME, SAL FROM EMP WHERE DEPTNO=10

*

ERROR at line 1:

ORA-01789: query block has incorrect number of result columns



SQL> SELECT ENAME, SAL FROM EMP WHERE DEPTNO=10

  2  UNION ALL

  3  SELECT SAL, ENAME FROM EMP WHERE DEPTNO=10;

SELECT ENAME, SAL FROM EMP WHERE DEPTNO=10

       *

ERROR at line 1:

ORA-01790: expression must have same datatype as corresponding expression



SQL> SELECT JOB FROM EMP WHERE DEPTNO=10

  2  UNION ALL

  3  (SELECT JOB FROM EMP WHERE DEPTNO=20

  4  MINUS

  5  SELECT JOB FROM EMP WHERE DEPTNO=30)

  6  UNION ALL

  7  SELECT ENAME FROM EMP WHERE DEPTNO=10;


JOB

----------

MANAGER

PRESIDENT

CLERK

ANALYST

CLARK

KING

MILLER


7 rows selected.


SQL>

Friday, July 7, 2023

Class- 2 sql/plsql

KK EDU SOLUTIONS

 




SQL Concepts: 20-25 Days
------------
1. Datatypes
2. SQL Commands
3. SQL Operators
4. Constraints
5. Joins
6. SQL Functions
7. Views and materialized views
8. Indexes
9. Sequences
10. Synonyms
11. SQL Loader

PLSQL Concepts: 18-20 Days
--------------
1. Anonomous or Unnamed blocks
2. Named blocks
   (i) Stored procedures
   (ii) Functions
   (iii) Packages
   (iv) Triggers
3. Collections, exceptions, cursors



Interview keys avilable 1500 rs 
if you intrested click link

class-1 pl sql

KK EDU SOLUTIONS


 





notes:-




sql command text -1


SQL Commands:
------------
1. DDL(Data definition language)
   (i) create
   (ii) alter
   (iii) rename
   (iv) modify
   (v) add
   (vi) truncate
   (vii) drop

2. DML(Data manipulation language)
   (i) insert
   (ii) update
   (iii) delete
   (iv) merge

3. TCL(Transaction control language)
   (i) commit
   (ii) rollback
   (iii) savepoint

4. DCL(Data control language)
   (i) grant
   (ii) revoke

5. DQL(Data query language)
   (i) select 

sql*plus, sql developer, toad, plsql developer



SQL*Plus: Release 11.2.0.2.0 Production on Mon Jun 5 09:19:43 2023

Copyright (c) 1982, 2014, Oracle.  All rights reserved.

SQL> conn system/system
ERROR:
ORA-01017: invalid username/password; logon denied


SQL> conn sys as sysdba
Enter password:
Connected.
SQL>
SQL> alter user system identified by system;

User altered.

SQL> conn system/system
Connected.
SQL>
SQL> connect
Enter user-name: system
Enter password:
Connected.
SQL> cl scr;


SQL> CREATE TABLE EMPLOYES
  2  (EMPNO NUMBER(4),
  3  ENAME VARCHAR2(10),
  4  SALARY NUMBER(6),
  5  JOB VARCHAR2(10),
  6  DEPTNO NUMBER(2));

Table created.

SQL> DESC EMPLOYES;
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 EMPNO                                              NUMBER(4)
 ENAME                                              VARCHAR2(10)
 SALARY                                             NUMBER(6)
 JOB                                                VARCHAR2(10)
 DEPTNO                                             NUMBER(2)

SQL> ALTER TABLE EMPLOYES
  2  RENAME COLUMN EMPNO TO ENO;

Table altered.

SQL> DESC EMPLOYES;
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 ENO                                                NUMBER(4)
 ENAME                                              VARCHAR2(10)
 SALARY                                             NUMBER(6)
 JOB                                                VARCHAR2(10)
 DEPTNO                                             NUMBER(2)

SQL> ALTER TABLE EMPLOYES
  2  MODIFY ENAME VARCHAR2(20);

Table altered.

SQL> DESC EMPLOYES;
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 ENO                                                NUMBER(4)
 ENAME                                              VARCHAR2(20)
 SALARY                                             NUMBER(6)
 JOB                                                VARCHAR2(10)
 DEPTNO                                             NUMBER(2)

SQL> ALTER TABLE EMPLOYES
  2  ADD AGE NUMBER(2);

Table altered.

SQL> DESC EMPLOYES;
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 ENO                                                NUMBER(4)
 ENAME                                              VARCHAR2(20)
 SALARY                                             NUMBER(6)
 JOB                                                VARCHAR2(10)
 DEPTNO                                             NUMBER(2)
 AGE                                                NUMBER(2)

SQL> ALTER TABLE EMPLOYES
  2  MODIFY AGE NUMBER(3);

Table altered.

SQL> DESC EMPLOYES;
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 ENO                                                NUMBER(4)
 ENAME                                              VARCHAR2(20)
 SALARY                                             NUMBER(6)
 JOB                                                VARCHAR2(10)
 DEPTNO                                             NUMBER(2)
 AGE                                                NUMBER(3)

SQL> ALTER TABLE EMPLOYES
  2  DROP COLUMN DEPTNO;

Table altered.

SQL> DESC EMPLOYES;
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 ENO                                                NUMBER(4)
 ENAME                                              VARCHAR2(20)
 SALARY                                             NUMBER(6)
 JOB                                                VARCHAR2(10)
 AGE                                                NUMBER(3)

SQL> RENAME EMPLOYES TO EMPLOYES33;

Table renamed.

SQL> DESC EMPLOYES;
ERROR:
ORA-04043: object EMPLOYES does not exist


SQL> DESC EMPLOYES33;
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 ENO                                                NUMBER(4)
 ENAME                                              VARCHAR2(20)
 SALARY                                             NUMBER(6)
 JOB                                                VARCHAR2(10)
 AGE                                                NUMBER(3)

SQL>




Friday, June 9, 2023

PL/SQL SESSION 2

KK EDU SOLUTIONS

PL/SQL SESSION 2




PL/SQL - Overview

The PL/SQL programming language was developed by Oracle Corporation in the late 1980s as procedural extension language for SQL and the Oracle relational database.

 Following are certain notable facts about PL/SQL −

  • PL/SQL is a completely portable, high-performance transaction-processing language.

  • PL/SQL provides a built-in, interpreted and OS independent programming environment.

  • PL/SQL can also directly be called from the command-line SQL*Plus interface.

  • Direct call can also be made from external programming language calls to database.

  • PL/SQL's general syntax is based on that of ADA and Pascal programming language.

  • Apart from Oracle, PL/SQL is available in TimesTen in-memory database and IBM DB2.

Features of PL/SQL

PL/SQL has the following features −

  • PL/SQL is tightly integrated with SQL.
  • It offers extensive error checking.
  • It offers numerous data types.
  • It offers a variety of programming structures.
  • It supports structured programming through functions and procedures.
  • It supports object-oriented programming.
  • It supports the development of web applications and server pages.

Advantages of PL/SQL

PL/SQL has the following advantages −

  • SQL is the standard database language and PL/SQL is strongly integrated with SQL. PL/SQL supports both static and dynamic SQL. Static SQL supports DML operations and transaction control from PL/SQL block. In Dynamic SQL, SQL allows embedding DDL statements in PL/SQL blocks.

  • PL/SQL allows sending an entire block of statements to the database at one time. This reduces network traffic and provides high performance for the applications.

  • PL/SQL gives high productivity to programmers as it can query, transform, and update data in a database.

  • PL/SQL saves time on design and debugging by strong features, such as exception handling, encapsulation, data hiding, and object-oriented data types.

  • Applications written in PL/SQL are fully portable.

  • PL/SQL provides high security level.

  • PL/SQL provides access to predefined SQL packages.

  • PL/SQL provides support for Object-Oriented Programming.

  • PL/SQL provides support for developing Web Applications and Server Pages.

PREVIOUS                                  NOTES                                            NEXT

Thursday, June 8, 2023

PL/SQL SESSION 1

KK EDU SOLUTIONS

 

PL/SQL



PL/SQL SESSION 1

PL/SQL Tutorial


PL/SQL is a combination of SQL along with the procedural features of programming languages. 

It was developed by Oracle Corporation in the early 90's to enhance the capabilities of SQL.

 PL/SQL is one of three key programming languages embedded in the Oracle Database, along with SQL itself and Java. 

This tutorial will give you great understanding on PL/SQL to proceed with Oracle database and other advanced RDBMS concepts.

Audience

This tutorial is designed for Software Professionals, who are willing to learn PL/SQL Programming Language in simple and easy steps. 

This tutorial will give you great understanding on PL/SQL Programming concepts, and after completing this tutorial, you will be at an intermediate level of expertise from where you can take yourself to a higher level of expertise.

Prerequisites

Before proceeding with this tutorial, you should have a basic understanding of software basic concepts like what is database, source code, text editor and execution of programs, etc.

 If you already have an understanding on SQL and other computer programming language, then it will be an added advantage to proceed.

PREVIOUS                                  NOTES                                            NEXT

Our Team

  • Syed Faizan AliMaster / Computers
  • Syed Faizan AliMaster / Computers
  • Syed Faizan AliMaster / Computers
  • Syed Faizan AliMaster / Computers
  • Syed Faizan AliMaster / Computers
  • Syed Faizan AliMaster / Computers