Skip to main content

Important Queries in SQL Part3 #DOTC_Mdb

Important Queries in SQL Part3.


In my previous blogs I discussed about several sql queries which commonly used in SQL and today also I will discuss about some important queries in SQL . Before that I suggest you to go through my SQL and PL/SQL blogs in archive May and June folder for your understanding.  Also you can see my below previous blog about Queries in SQL.

 https://dheeraj60.blogspot.com/2020/07/important-queries-in-sql-part1-dotcmdb.html

https://dheeraj60.blogspot.com/2020/07/important-sql-queries-part-2-dotcmdb.html


1. Self Join Example :  Suppose I have an employee table and need to fetch Employee_name and his Manager_name from one table only. As we know self Join is used to join the table itself . So finding this we need to write below query.

Select a.emp_name as Employee,b.emp_name as Manager From

Employee a,Employee b where a.emp_id=b.Manager_id;

This query is important as we join the table itself and get the requirement.

Below is the output of above query in image :


 Output of Employee name as well as Manager name is shown in output.


2.  Write a Query to fetch employee data which exist in employee_salary table .

SELECT * FROM Employee a WHERE

EXISTS

(SELECT * FROM Employee_Salary b WHERE a.emp_Id = b.Emp_Id);

 

Here we use subquery to get the required data and use EXISTS keyword. Please refer my Subquery and co-related subquery blog for your reference.

Below is the output of above query.


3. Find out the Duplicate rows in a table without using rowid and distinct in SQL.

 

For this we can use aggregate function with group by and Having clause in SQL query .

 

SELECT emp_id, emp_name, Salary,

 COUNT(*) FROM Employee_Salary

 GROUP BY Emp_id, emp_name, Salary

 HAVING COUNT(*) > 1;

Below is the output :

Here we will get no result as in my table there is no duplicate records.


You can check with inserting duplicate records in table . 

4. How to find current date in database in SQL :

In oracle we use sysdate . Below is the query

select sysdate from dual;

In My SQL we can use below query to see the current date :

Select now();

Below is the output of above query :

Note : This functions is very important to find the current date in SQL query and can be used anywhere inside the long query as well.

5. How to get the values from employee table where name starts with D.

 

For this we  need to use the like operator to fetch the above requirement from employee table . Below is the Query

Select * from employee

 where emp_name

Like 'D%';

Below is the output of above query :


 

6.  SQL query to print the emp_name from employee table after removing white spaces from the right side.

 

In SQL we can use RTRIM function to retrieve the vaue . Below is the query .

Select RTRIM(emp_name) from employee;

Same we can apply for removing white spaces from left side by using LTRIM . Below is the Query.

Select LTRIM(emp_name) from employee;

Below is the output of above query :

 

 

7 . Create one table by taking data from two table with common records no need to declare data type as it will directly take it from both the table .

 

For this we simply need to use equi/inner Join and through create statement we can get this requirement.

Below is the query :

Create table employee1

As
select a.emp_name, a.salary , b.location

From employee a , department b

Where a.emp_id=b.emp_id;

 

Here new table is created as employee1 without declaring any data types and also we can get common records from both the table.

Below is the output for your reference.

Now we can see the data output also in new table employee1 . 

Hope you are able to understand this queries which is commonly used and asked in interview. Please try to practice this query and also see my previous blogs for queries and let me know if you have any doubt.

Please go through this blog carefully as in next blog I will come up with new topics related to complex queries in SQL.

Thanks.

 

Comments

Popular posts from this blog

SQL and Classification of SQL in Oracle Database #Diksha Oracle Training Centre

  SQL and Classification of SQL in Oracle Database.   SQL is Structured Query Language , which is used for storing, manipulating and retrieving data stored in a relational database .SQL is the standard language for RDBMS. All the Relational Database Management Systems (RDMS) like Oracle, MySQL, Sybase, Informix, IBM DB2 and Microsoft SQL Server use SQL as their standard database language. Oracle is one of the more secured database as compared to other databases. Importance of   SQL : SQL and PL/SQL is a backend process where all data is stored and retrieved in GUI which created either by any programming languages like Java, C++, PHP etc. so we need to have very secure database so that there will be no impact for users. SQL allows users to access data in the relational database management systems. SQL is used to communicate with a database.SQL and PL/SQL allows users to create and drop databases tables , views , stored procedures , functions , packages , trigger et...

Dynamic SQL in Oracle PL/SQL #DOTC_Mdb

                                    Dynamic SQL in Oracle PL/SQL Hello Everyone, Today we will discuss about Dynamic SQL in Oracle PL/SQL and its benefits. Before reading this blog I will suggest you to see my previous blogs related with SQL and PL/SQL blog for better understanding as some of PL/SQL feature I will use in this blog. Let’s start to understand Dynamic SQL in Oracle. Dynamic SQL in Oracle PL/SQL used for generating and running SQL statements at run time. Through Dynamic SQL we can use DDL statements in PL/SQL block.   Dynamic SQL is useful when writing general-purpose and flexible programs like ad hoc query systems, when writing programs that must run database definition language (DDL) statements. We can write Dynamic SQL in two ways. 1. Native Dynamic SQL 2. DBMS_SQL 1. Native dynamic SQL in PL/SQL language enables for building and running dynamic SQL statements at ru...

Concept of Software Development life Cycle in IT industry #DOTC_Mdb

Concept of Software Development life Cycle in IT industry.   Hello Everyone, In my previous blog we discussed about Agile Model which is also a part of SDLC, but today we discussed about Software Development life cycle in details and how being software products developed or services provided through SDLC. I suggest you to please go through my previous blog that is related to Agile model also a part of SDLC which permits a company to develop software in small, quick segments. https://dheeraj60.blogspot.com/2020/07/overview-of-agile-modelmethodology.html Let’s discuss about SDLC as its play very important role in developing software products as well as providing services to the clients in simple word we can say SDLC(Software Development life Cycle ) is a process that produces software with the highest quality and lowest cost in the shortest time. SDLC includes a detailed plan for how to develop, alter, maintain, and replace a software system .SDLC is also known as Software devel...