Skip to main content

Important SQL Queries in SQL Part 6 #DOTC_Mdb

Important SQL Queries in SQL Part 6.

In my previous blogs I discussed about several SQL queries which commonly used in SQL and today again 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.

 

I suggest you before Starting the queries please go through my blog Sub query /Co-related sub query and different logical operator for your understanding.

1.  Co- Related Sub Query Example with other ALL operator : Let’s take an example for a typical sub query using ALL operator. In this example, we need to return the names of employee from department table  whose salary is greater than the salary of all the employees in department 100.

 

Below is the Query :

SELECT emp_name

FROM department

WHERE Salary > ALL ( SELECT Salary

FROM department

WHERE Dept_no=100 );

 

Below is the output of above query:

 

 

 

2. Find the manager_ID whose maximum salary among all employees of that manager_id  is greater than salary average  of all manager_id.

 

Below is the Query for that :

 

SELECT manager_id

FROM department

GROUP BY dept_no

HAVING max(salary) > ALL (SELECT avg(salary)

                            FROM department

                            GROUP BY dept_no);

 

Output of the above Query is as below :



3 . Using Any Operator : Find the Distinct dept_no of the emp_id which have any record in Department Table and location not in Delhi and Chennai.

 

SELECT DISTINCT dept_no

FROM department

WHERE emp_id = ANY (SELECT emp_id

 FROM department

 where Location NOT IN( 'DELHI','Chennai'));

Below is the output of above query :

 

Here we see all the distinct dept_no apart from 100 as in outer query we put location not belongs to DELHI and Chennai as both belongs to dept_no 100

Note : You can write query against your requirement by taking reference from this queries.

4 . How to add / modify / Delete column in existing table .

To add column in existing table we can do it by using ALTER DDL statement.

Suppose I want to add last name is existing table department then below statement we can use.

ALTER TABLE DEPARTMENT ADD COLUMN LAST_NAME VARCHAR(50);

To Modify the data type of column we can also use the Alter statement as below :

ALTER TABLE DEPARTMENT MODIFY LAST_NAME VARCHAR(40);

See the output of above statements.

 

Here you can see that one column is added and second command modify the data type of column from VARCHAR(50) to VARCHAR(40).

In same Manner you can Drop the column by using ALTER statement.

ALTER TABLE DEPARTMENT DROP COLUMN LAST_NAME;

5 . How to Add constraint like Primary Key / Foreign Key NOT NULL in Existing table.

 

To add primary key in existing table we use below DDL statement :

ALTER TABLE STUDENT ADD PRIMARY KEY (STUD_ID);

To add Foreign Key we use below statement in existing table :

ALTER TABLE STUDENT_DETAILS ADD FOREIGN KEY (STUD_ID) REFERENCES STUDENT (STUD_ID);

Below is the output of above statement.

 

Note :we created constraint Primary key on stud_id in Student table , So in Sudent table for stud_id we cannot insert any duplicate values as well as NULL values also we created foreign key on stud_id in student_details table . so in student_details table we can insert multiple values which exist in primary table but unable to insert any other record . We can say this as Parent child relations. If you want to explore more on this please follow my previous blog for more details .

ADDING NOT NULL CONSTRAINT IN EXISTING TABLE :

ALTER TABLE STUDENT MODIFY COLUMN STUDENT_ADDRESS VARCHAR(60) NOT NULL;

Now After issuing this command user cannot enter any null values.

Please go through this blog carefully and try to write as many queries as you can and also use constraint to restrict the user as per your requirement and let me know if you have any doubts. I will suggest you to please go through my previous blogs related to SQL topics for your reference.

Thanks.


Comments

Post a Comment

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 etc. SQL allows

Materialized View uses and Benefits in Database #DOTC_Mdb

Materialized View uses and Benefits in Database. Hello Everyone, Today we will discuss about Materialized view as it’s play important role in database. We already discussed about Simple Views and complex views in my previous blog. Before Materialized view go through my previous blog which related to simple view. https://dheeraj60.blogspot.com/2020/05/benefits-of-creating-and-using-view-in.html As we know View is not a database object and not like table which is stored in database, but view can be created from base table a view is an SQL statement that’s stored in the database. This statement, or view, has a name.A view looks and acts a lot like a table. It has columns and rows, and can be included in SELECT queries just like a table. In other word we can say View is a virtual/logical table which is basically used for security purpose. Let’s understand   about   Materialized view : A materialized view is a view that stores the results of the view’s query. Whenever you query the ma

Top 50 Interview Questions On SQL and PL/SQL #DOTC_Mdb

                    Top 50 Interview Questions On SQL and PL/SQL. Today we will Discuss Top 50 interview questions and answers of SQL and PL/SQL which is frequently asked in interview.     Question 1: What is SQL and Classification of SQL? Answer SQL is a Structure Query Language which is vastly used in RDBMS database like Oracle, Sybase, DB2 , Microsoft SQL server etc.   Classification of SQL is as below: DDL (Data Definition Language):  Commands are  create , alter , drop , truncate etc DML (Data Manipulation Language) : Commands are  insert,update and delete . TCL (Transaction Control Language ) : Commands are  Commit , Rollback and Save point. DCL (Data Control Language) : Commands are Grant , Revoke Question 2:    What is meant by Joins? What are the types of join? Answer Joins are basically used to extract/get data from multiple tables using some common columns or conditions and also by using alias to fulfill the condition.   There are various types of Joins as li