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 et...

PL/SQL in Oracle and basic Programs in PL/SQL #Diksha Oracle Training Centre

              PL/SQL in Oracle with basic Programs. Hello Everyone, Today I will discuss about PL/SQL in Oracle .In Previous Blog we discussed about cursor and anonymous block used in cursor as well as uses of %TYPE and %ROWTYPE attribute. Before starting this topic please go through my below blog. https://dikshaoracletraining.blogspot.com/2020/10/cursor-and-types-of-cursor-in-oracle.html https://dikshaoracletraining.blogspot.com/2020/11/benefits-of-type-and-rowtype-in-oracle.html PL/SQL is basically a Procedural Language in SQL and in PL/SQL we use separate two parts first is Anonymous Block and second is Named Block. The PL/SQL programming language was developed by Oracle Corporation in the late 1980s as procedural extension language for SQL relational database. PL/SQL high-performance transaction-processing language.PL/SQL is completely portable.   PL/SQL provides a built-in, interpreted and OS independent programming environment. ...

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 s...