[is0] Write a query to show all employee who all are in office.
select * from employee_inout e where status = 'IN' and not exists (select * from employee_inout where employee_id=e.employee_id and status='OUT' and timing>e.timing)
[is1] Write a count for below scenario using join: t1(1,1,2,2) t2(1,2)
select * from t1 natural join t2; --4
select * from t1 cross join t2 ; --8
select * from t1 join t2 using (column1); --4
select * from t1 join t2 on (t1.column1=t2.column1);--4
select * from t1 left outer join t2 on (t1.column1=t2.column1);--4
select * from t1 right outer join t2 on (t1.column1=t2.column1);--4
select * from t1 full outer join t2 on (t1.column1=t2.column1);--4
[cb0] Write a query to show data ordered based on country_name, but India need to come first and rest need to be ordered as usually.
SELECT country_id, country_name FROM countries ORDER BY CASE WHEN country_name = 'India' THEN 1 ELSE 2 END, country_name ASC;
[cb1] Write a query to print a list of comma separate values in new rows, input row is 'Rajasthan,Maharastra,Tamilnadu'
SELECT TRIM(REGEXP_SUBSTR('Rajasthan,Maharastra,Tamilnadu', '[^,]+', 1, LEVEL)) AS state FROM DUAL CONNECT BY REGEXP_SUBSTR('Rajasthan,Maharastra,Tamilnadu', '[^,]+', 1, LEVEL) IS NOT NULL;
[cb2] Write a query to print last forward slash value 'India/Norway/England/Dubai'?
SELECT SUBSTR('India/Norway/England/Dubai', INSTR('India/Norway/England/Dubai', '/', -1) + 1) AS last_value FROM dual;
[cb3] How to update tab1 col1 using tab2 col1, using below table structure:
tab1 (id, col1, col2)
tab2(id, col1, col2)
Option-01
UPDATE tab1 t1 SET t1.col1 = ( SELECT t2.col1 FROM tab2 t2 WHERE t1.id = t2.id)
WHERE EXISTS ( SELECT 1 FROM tab2 t2 WHERE t1.id = t2.id);
Option-02
MERGE INTO tab1 t1 USING tab2 t2 ON (t1.id = t2.id) WHEN MATCHED THEN UPDATE SET t1.col1 = t2.col1;
[cb4] How to reset sequence from 500 to 0?
To reset a sequence in Oracle from 500 to 0 and then start from 1 to n, you can use the following SQL commands:
ALTER SEQUENCE your_sequence_name
INCREMENT BY -500
MINVALUE 0;
ALTER SEQUENCE my_sequence_name INCREMENT BY 1;
[cb5] how to add 10 second in current system date time?
You can add 10 seconds to the current date and time in Oracle using the SYSDATE function along with an interval. Here's an example:
SELECT SYSDATE + INTERVAL '10' SECOND FROM DUAL;
[cb6] How to get hh:mi difference from col1 which is varchar and containing hh:mi values
and another col2 is timestamp and having full date and time?
SELECT col1, col2,
EXTRACT(hour FROM (TO_TIMESTAMP(col1, 'HH24:MI') - col2)) || ':' ||
EXTRACT(minute FROM (TO_TIMESTAMP(col1, 'HH24:MI') - col2)) AS diff_hh_mi
FROM your_table;
[cb7] Can we create table as dual?
No, we cannot create a table as dual in Oracle. The dual table is a special one-row, one-column table that is provided by Oracle for performing calculations or calling functions that do not require any table input. It is not a regular table that can be created or modified by the user. Instead, it is automatically created by Oracle when the database is installed, and it can be accessed by any user without any special privileges.
[cb8] How many are regular expression function?
Oracle provides a set of regular expression functions that can be used in SQL and PL/SQL. The following is a list of the regular expression functions available in Oracle:
- REGEXP_LIKE
- REGEXP_SUBSTR
- REGEXP_REPLACE
- REGEXP_INSTR
- REGEXP_COUNT
These functions allow you to search, extract, and manipulate text based on patterns defined using regular expressions.
[cb9] Write a query based on below scenario:
There is an emp table and having budget table to allocate fund 60% to superior and 40% junior based on rating.
CREATE TABLE budget(emp_id NUMBER,
fund_allocation NUMBER, total_fund NUMBER);
CREATE TABLE emp(emp_id NUMBER, rating VARCHAR(30));
MERGE INTO budget b USING emp e ON (e.emp_id = b.emp_id)
WHEN MATCHED THEN
UPDATE SET b.fund_allocation =
CASE WHEN e.rating = 'Superior' THEN b.total_fund * 0.6
WHEN e.rating = 'Junior' THEN b.total_fund * 0.4 ELSE 0 END;
Write a query to show data without special character from table column?
SELECT REGEXP_REPLACE(column_name, '[^[:alnum:]]') AS cleaned_column
FROM table_name;
Aggregate functions perform summary operations on a set of values to provide a single value. There are several aggregate functions that we use in our code to perform calculations. These are: AVG | MIN | MAX | COUNT |SUM
What are the set operators UNION, UNION ALL, MINUS & INTERSECT meant to do?
- UNION operator returns all the rows from both the tables except the duplicate rows.
- UNION ALL returns all the rows from both the tables along with the duplicate rows.
- MINUS returns rows from the first table, which does not exist in the second table.
- INTERSECT returns only the common rows in both the tables.
select employee_id, rownum from employees;select employee_id, rownum from employees where department_id in (30,20,10);select * from employees where rownum=10;select * from employees where rownum<=10;select min(Salary),max(salary) from (select * from employees where rownum<=10);
select * from (select distinct salary from employees order by salary desc)where rownum<=7;--select min(salary) from (select distinct salary from employees order by salary desc)where rownum<=7;--select min(salary) from (select distinct salary from employees order by salary desc)where rownum<=&nthsalary;
DELETE
The DELETE command is used to remove rows from a table. A WHERE clause can be used to only remove some rows. If no WHERE condition is specified, all rows will be removed. After performing a DELETE operation you need to COMMIT or ROLLBACK the transaction to make the change permanent or to undo it. Note that this operation will cause all DELETE triggers on the table to fire.
TRUNCATE removes all rows from a table. The operation cannot be rolled back and no triggers will be fired. As such, TRUCATE is faster and doesn't use as much undo space as a DELETE.
No comments:
Post a Comment