0% found this document useful (0 votes)
1K views3 pages

SQL Queries for Data Management

This document contains SQL queries and PL/SQL code snippets for common Oracle database tasks like finding the top salaries, finding duplicate records, deleting duplicate rows, extracting DDL, compiling objects, using the MERGE statement, deleting duplicate records, and exporting/importing data between databases. Queries are provided to find the top salaries, check for duplicate records, delete duplicate rows, get user details, drop columns, extract DDL, compile objects, demonstrate the MERGE statement syntax, and delete duplicate records from a table. Code snippets also show how to export and import data between databases.

Uploaded by

sivaniranjan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views3 pages

SQL Queries for Data Management

This document contains SQL queries and PL/SQL code snippets for common Oracle database tasks like finding the top salaries, finding duplicate records, deleting duplicate rows, extracting DDL, compiling objects, using the MERGE statement, deleting duplicate records, and exporting/importing data between databases. Queries are provided to find the top salaries, check for duplicate records, delete duplicate rows, get user details, drop columns, extract DDL, compile objects, demonstrate the MERGE statement syntax, and delete duplicate records from a table. Code snippets also show how to export and import data between databases.

Uploaded by

sivaniranjan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
  • Common SQL Queries: Provides examples of SQL queries for operations like finding top salaries, detecting duplicates, and deleting duplicate rows.
  • Extracting DDL Scripts: Instructions on how to extract DDL scripts for tables, views, and indexes, and how to compile database objects.
  • Export/Import Procedures: Covers the methods for exporting and importing data between databases, especially for testing purposes.

3 highest sal:

select top 3 sal from employee order by sal;


select * from (select * from emp order by sal) where rownum<3 ;
select * from (select distinct salary from ran1 order by salary desc)where rown
um<=3;;
To find duplicates in table:
SELECT COUNT(*), COLUMN_A, COLUMN_B
FROM TABLE_B
GROUP BY COLUMN_A, COLUMN_B
HAVING COUNT(*)>1;
or
SELECT * FROM emp a
WHERE rowid = (SELECT max(rowid) FROM emp
WHERE empno = a.empno
GROUP BY empno
HAVING count(*) >1)
i want to select records that have same duplicate id in table A?
select id from A group by id having count(*) > 1
to delete duplicate rows:
delete from
table_name a
where
a.rowid >
any (select b.rowid
from
table_name b
where
a.col1 = b.col1
and
a.col2 = b.col2
)
;

To list the username and size of the user:


select owner,sum(bytes) from dba_segments group by owner;
To know the full details about the user:
select * from dba_users where username= scott ;
How to Drop Columns from a Table?
ALTER TABLE &TABLE_NAME SET UNUSED &COLUMN_NAME; -- YOU CAN ADD SAME COLUMN WIT
H SAME NAME AS OF UNUSED COLUMN
SELECT * FROM USER_UNUSED_COL_TABS;
ALTER TABLE &TABLE_NAME DROP UNUSED COLUMNS;

How to Extract the DDL script for a Table, Materialized View or even an Index
=============================================================================
=========== EXTRACT DDL OF TABLE/MVIEW AND INDEX==============================
=============================================================================
SET LINES 300 PAGES 300
SET LONG 30000
SELECT DBMS_METADATA.GET_DDL('TABLE','&OBJECT_NAME') FROM DUAL;
SELECT DBMS_METADATA.GET_DDL('INDEX','&OBJECT_NAME') FROM DUAL;
How to Complie all database Objects
===============================================================================
====================COMPILE ALL DATABASE STORED OBJECTS=======================
===============================================================================
SET HEADING OFF
SET PAGESIZE 0
SET LINESIZE 79
SET VERIFY OFF
SET ECHO OFF
SELECT
DECODE( OBJECT_TYPE, 'PACKAGE BODY',
'ALTER PACKAGE ' || OWNER||'.'||OBJECT_NAME || ' COMPILE BODY;',
'ALTER ' || OBJECT_TYPE || ' ' || OWNER||'.'||OBJECT_NAME || ' COMPILE;' )
FROM ALL_OBJECTS A WHERE STATUS = 'INVALID' AND
OBJECT_TYPE IN ( 'PACKAGE BODY', 'PACKAGE', 'FUNCTION', 'PROCEDURE','TRIGGER', '
VIEW' )
ORDER BY OBJECT_TYPE, OBJECT_NAME;
Syntax for MERGE Statement in Oracle PL-SQL
=============================================================================
==================== MERGE STATEMENT =======================================
=============================================================================
MERGE STATEMENT - INSERT OR UPDATE ("UPSERT")
merge is a new sql statement introduced in oracle9i to update or insert rows sel
ected from one
table to another based on a condition. this avoids writing multiple dml statemen
ts to satisfy a
conditional update or insert. the syntax is:
MERGE [HINT] INTO [SCHEMA .] TABLE [T_ALIAS] USING [SCHEMA .]
{ TABLE | VIEW | SUBQUERY } [T_ALIAS] ON ( CONDITION )
WHEN MATCHED THEN UPDATE SET COLUMN = { EXPR | DEFAULT } [, COLUMN = { EXPR | DE
FAULT }]...
WHEN NOT MATCHED THEN INSERT ( COLUMN [, COLUMN]... ) VALUES ( EXPR [, EXPR]...
)
How to Delete Duplicate Records from a table
======================================================================
================= DELETE DUPLICATE ROWS FROM A TABLE =================
======================================================================
undefine tablename
undefine column_name -- COLUMN OF TABLE WITH PRIMARY KEY OR UNIQUE VALUES
DELETE FROM &&TABLENAME

WHERE ROWID NOT IN (SELECT MIN(ROWID) FROM &&TABLENAME GROUP BY &COLUMN_NAME);


PL-SQL Syntax for Exporting and Importing data from One Database to Another
========================================================================
================== EXPORT /IMPORT
=====================================
=========================================================================
PURPOSE : SOMETIMES FOR TESTING WE NEED TO COPY DATA FROM ONE DB TO OTHER
CREATE TABLE &TABLE_NAME PARALLEL 2 AS SELECT * FROM &EXISTING_TABLE;
$ exp tables=table_name file=filename.dmp rows=y compress=y direct=y
NOW MOVE FILENAME.DMP TO DESIRE MACHINE AND
$imp file=filename.dmp full=y

You might also like