SlideShare a Scribd company logo
1 of 23
5 Surprising Oracle SQL
Behaviors That Very Few
People Know
Question #1 for an Oracle SQL expert
Which of the queries below contain the rows with Null Salary?
SELECT * FROM Persons WHERE SALARY > 1000
SELECT * FROM Persons WHERE SALARY <= 1000
Answer
In fact, the conditions
both implicitly assume that SALARY is not NULL.
SALARY > 1000 and SALARY <= 1000
None of them
All logical operators are FALSE when one operand is NULL
NULL = 1
NULL != 1
NULL > 1
NULL = "ABC"
NULL != "ABC"
NULL LIKE "Steve%"
NULL NOT LIKE "Steve%"
NULL = NULL
All the below examples return FALSE
How do we get the rows where SALARY is NULL?
We use the operator IS NULL
SELECT * FROM Persons WHERE SALARY IS NULL
How to check whether a value is Null or Not?
Recall from earlier, the below conditions are always FALSE
โ€ข COL = NULL
โ€ข COL != NULL
Use the below operators instead
โ€ข IS NULL
โ€ข IS NOT NULL
Question #2 for an Oracle SQL expert
Which condition detects a zero-length character field?
WHERE COL = โ€˜โ€™
WHERE Length(COL) = 0
Answer
โ€ข Neither 1
โ€ข Neither 2
Explanations
โ€ข In Oracle character fields cannot be of zero length.
โ€ข Oracle substitutes NULL to a โ€˜โ€™ literal in Queries
โ€ข When we insert a โ€˜โ€™ in a cell , NULL gets inserted
โ€ข The condition X = โ€˜โ€™ is interpreted as X = NULL -> a condition that
is always false.
โ€ข So, to test if a Col is empty, we test if itโ€™s null:
โ€ข Length(COL) = 0 does not work, since Length(NULL) is NULL.
โ€ข The correct condition based on length is:
โ€ข So, itโ€™s more concise to use the first condition.
WHERE COL IS NULL
WHERE Length(COL) IS NULL
Question#3 for an Oracle SQL expert
What does the queries below do?
SELECT NAME, age, weight / ( height * height )
AS BMI FROM persons ORDER BY BMI
SELECT NAME, age, weight / ( height * height )
AS BMI FROM persons WHERE BMI > 25
Answer
They both raise errors
Oracle does not accept using a column alias in Where or Order by
clauses.
Solution 1
Repeat the definitions of the aliases when needed
SELECT NAME, age, weight / ( height * height ) AS BMI FROM
persons ORDER BY weight / ( height * height)
SELECT NAME, age, weight / ( height * height ) AS BMI FROM
persons WHERE weight / ( height * height) > 25
Cons:
โ€ข Too much redundancy.
โ€ข Query is too long.
โ€ข Every time we change one alias, we must modify it definition in several
locations.
Solution 2
This method is generic and works in all cases
SELECT * FROM
(SELECT NAME, age, weight / (height * height) AS BMI FROM PERSONS)
WHERE BMI > 25
SELECT * FROM
(SELECT NAME, age, weight / (height * height) AS BMI FROM PERSONS)
ORDER BY BMI
The part between parentheses acts as a new temporary table, that now has a column
named after each alias. Now, we can use the aliases in the WHERE and ORDER BY
expressions.
Question #4 for an Oracle SQL expert
What does the following requests return?
SELECT ColX, * from MYTABLE
SELECT ColX, * , ColZ from MYTABLE
Answer
Oracle, only accepts the asterisk when itโ€™s the only thing between the Select and From, as in:
Select * from MYTABLE
If we mix, columns with asterisk, (as in the queries below) Oracle will not
understand the syntax and will raise errors.
Select ColX, * from MYTABLE
Select ColX, *, ColZ from MYTABLE
Good news! Oracle will accept the syntax if we simply prefix the
asterisk symbol with the table name (or table alias name), as
shown below:
Select colX, MYTABLE.* from MYTABLE
Select colX, m.* from MYTABLE m
Question #5 for an Oracle SQL expert
Which of the below queries gets the top N rows from a table?
Select TOP N * from MYTABLE
Select * from MYTABLE LIMIT 10
Answer
โ€ข They both fail.
โ€ข Syntax 1 works on SQL Server
โ€ข Syntax 2 works on MySQL
โ€ข On Oracle, we must use the pseudo-column rownum, that is a virtual
column that represents the line number.
Select * from MYTABLE where ROWNUM <=N
โ€ข So, if we want to get the first 3 rows, we can use:
โ€ข Rownum can also be included in the list of fields, allowing us to display the number
of each row:
Select * from MYTABLE where rownum < 4
Select rownum, MYTABLE.* from MYTABLE where rownum < 4
Beware of rownum pitfall #1
Remember that filtering is executed before the sorting
will sort the list containing the first 3 students in the table - Not very useful.
The proper way to get the top N rows of a sorted query is to use an inline
view as shown below:
Select Name, Grade from Students where rownum < 4 order by grade
SELECT * FROM
(SELECT Name, Grade FROM Students ORDER BY Grade)
WHERE ROWNUM < 4
Beware of rownum pitfall 2
The first row that breaks the condition on rownum will stop the execution
of the query. The below conditions, for example, will yield no results
since they fail on the first row of the table.
ROWNUM > 1
ROWNUM = 3
There is a workaround for this limitation by using an inline view
containg rownum.
Select * from
(Select ROWNUM AS lineNb, mytabe.* from mytable WHERE ROWNUM < 4)
WHERE lineNb = 3
Slides prepared by
Boutros CHALOUHY

More Related Content

Similar to 5 surprising oracle sql behaviors that very few people know

SQL Lesson 6 - Select.pdf
SQL Lesson 6 - Select.pdfSQL Lesson 6 - Select.pdf
SQL Lesson 6 - Select.pdfMadhusha15
ย 
Database object, sub query, Join Commands & Lab Assignment
Database object, sub query, Join Commands & Lab AssignmentDatabase object, sub query, Join Commands & Lab Assignment
Database object, sub query, Join Commands & Lab AssignmentArun Sial
ย 
Null Values.ppt briefing about null values in SQL. Very helpful if you are le...
Null Values.ppt briefing about null values in SQL. Very helpful if you are le...Null Values.ppt briefing about null values in SQL. Very helpful if you are le...
Null Values.ppt briefing about null values in SQL. Very helpful if you are le...shalinigambhir3
ย 
Data Base Management System Lecture 10.pdf
Data Base Management System Lecture 10.pdfData Base Management System Lecture 10.pdf
Data Base Management System Lecture 10.pdfhowto4ucontact
ย 
Chinabankppt
ChinabankpptChinabankppt
Chinabankpptnewrforce
ย 
Sql operators & functions 3
Sql operators & functions 3Sql operators & functions 3
Sql operators & functions 3Dr. C.V. Suresh Babu
ย 
10053 - null is not nothing
10053 - null is not nothing10053 - null is not nothing
10053 - null is not nothingHeribertus Bramundito
ย 
SQL Keywords and Functions.pptx
SQL Keywords and Functions.pptxSQL Keywords and Functions.pptx
SQL Keywords and Functions.pptxRUBAB79
ย 
Unit 3-Select Options and Aggregate Functions in SQL (1).pptx
Unit 3-Select Options and Aggregate Functions in SQL (1).pptxUnit 3-Select Options and Aggregate Functions in SQL (1).pptx
Unit 3-Select Options and Aggregate Functions in SQL (1).pptxHAMEEDHUSSAINBU21CSE
ย 
Practice on Practical SQL
Practice on Practical SQLPractice on Practical SQL
Practice on Practical SQLHideshi Ogoshi
ย 
Sql performance vesl technologies
Sql performance vesl technologiesSql performance vesl technologies
Sql performance vesl technologiesSuresh Mishra
ย 
Beginers guide for oracle sql
Beginers guide for oracle sqlBeginers guide for oracle sql
Beginers guide for oracle sqlN.Jagadish Kumar
ย 
Sql intro
Sql introSql intro
Sql introglubox
ย 
Les02 Restricting and Sorting Data using SQL.ppt
Les02 Restricting and Sorting Data using SQL.pptLes02 Restricting and Sorting Data using SQL.ppt
Les02 Restricting and Sorting Data using SQL.pptDrZeeshanBhatti
ย 

Similar to 5 surprising oracle sql behaviors that very few people know (20)

SQL Lesson 6 - Select.pdf
SQL Lesson 6 - Select.pdfSQL Lesson 6 - Select.pdf
SQL Lesson 6 - Select.pdf
ย 
0808.pdf
0808.pdf0808.pdf
0808.pdf
ย 
0808.pdf
0808.pdf0808.pdf
0808.pdf
ย 
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Chapter 2  grouping,scalar and aggergate functions,joins   inner join,outer joinChapter 2  grouping,scalar and aggergate functions,joins   inner join,outer join
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
ย 
Database object, sub query, Join Commands & Lab Assignment
Database object, sub query, Join Commands & Lab AssignmentDatabase object, sub query, Join Commands & Lab Assignment
Database object, sub query, Join Commands & Lab Assignment
ย 
Null Values.ppt briefing about null values in SQL. Very helpful if you are le...
Null Values.ppt briefing about null values in SQL. Very helpful if you are le...Null Values.ppt briefing about null values in SQL. Very helpful if you are le...
Null Values.ppt briefing about null values in SQL. Very helpful if you are le...
ย 
Data Base Management System Lecture 10.pdf
Data Base Management System Lecture 10.pdfData Base Management System Lecture 10.pdf
Data Base Management System Lecture 10.pdf
ย 
Chinabankppt
ChinabankpptChinabankppt
Chinabankppt
ย 
Sql operators & functions 3
Sql operators & functions 3Sql operators & functions 3
Sql operators & functions 3
ย 
SQL- Introduction to MySQL
SQL- Introduction to MySQLSQL- Introduction to MySQL
SQL- Introduction to MySQL
ย 
Mysql Optimization
Mysql OptimizationMysql Optimization
Mysql Optimization
ย 
10053 - null is not nothing
10053 - null is not nothing10053 - null is not nothing
10053 - null is not nothing
ย 
SQL Keywords and Functions.pptx
SQL Keywords and Functions.pptxSQL Keywords and Functions.pptx
SQL Keywords and Functions.pptx
ย 
DBMS.pptx
DBMS.pptxDBMS.pptx
DBMS.pptx
ย 
Unit 3-Select Options and Aggregate Functions in SQL (1).pptx
Unit 3-Select Options and Aggregate Functions in SQL (1).pptxUnit 3-Select Options and Aggregate Functions in SQL (1).pptx
Unit 3-Select Options and Aggregate Functions in SQL (1).pptx
ย 
Practice on Practical SQL
Practice on Practical SQLPractice on Practical SQL
Practice on Practical SQL
ย 
Sql performance vesl technologies
Sql performance vesl technologiesSql performance vesl technologies
Sql performance vesl technologies
ย 
Beginers guide for oracle sql
Beginers guide for oracle sqlBeginers guide for oracle sql
Beginers guide for oracle sql
ย 
Sql intro
Sql introSql intro
Sql intro
ย 
Les02 Restricting and Sorting Data using SQL.ppt
Les02 Restricting and Sorting Data using SQL.pptLes02 Restricting and Sorting Data using SQL.ppt
Les02 Restricting and Sorting Data using SQL.ppt
ย 

Recently uploaded

W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
ย 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
ย 
CALL ON โžฅ8923113531 ๐Ÿ”Call Girls Kakori Lucknow best sexual service Online โ˜‚๏ธ
CALL ON โžฅ8923113531 ๐Ÿ”Call Girls Kakori Lucknow best sexual service Online  โ˜‚๏ธCALL ON โžฅ8923113531 ๐Ÿ”Call Girls Kakori Lucknow best sexual service Online  โ˜‚๏ธ
CALL ON โžฅ8923113531 ๐Ÿ”Call Girls Kakori Lucknow best sexual service Online โ˜‚๏ธanilsa9823
ย 
CHEAP Call Girls in Pushp Vihar (-DELHI )๐Ÿ” 9953056974๐Ÿ”(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )๐Ÿ” 9953056974๐Ÿ”(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )๐Ÿ” 9953056974๐Ÿ”(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )๐Ÿ” 9953056974๐Ÿ”(=)/CALL GIRLS SERVICE9953056974 Low Rate Call Girls In Saket, Delhi NCR
ย 
call girls in Vaishali (Ghaziabad) ๐Ÿ” >เผ’8448380779 ๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธ
call girls in Vaishali (Ghaziabad) ๐Ÿ” >เผ’8448380779 ๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธcall girls in Vaishali (Ghaziabad) ๐Ÿ” >เผ’8448380779 ๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธ
call girls in Vaishali (Ghaziabad) ๐Ÿ” >เผ’8448380779 ๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธDelhi Call girls
ย 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
ย 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
ย 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfWilly Marroquin (WillyDevNET)
ย 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
ย 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlanโ€™s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlanโ€™s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlanโ€™s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlanโ€™s ...OnePlan Solutions
ย 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
ย 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
ย 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
ย 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
ย 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
ย 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
ย 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
ย 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
ย 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
ย 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto Gonzรกlez Trastoy
ย 

Recently uploaded (20)

W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
ย 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
ย 
CALL ON โžฅ8923113531 ๐Ÿ”Call Girls Kakori Lucknow best sexual service Online โ˜‚๏ธ
CALL ON โžฅ8923113531 ๐Ÿ”Call Girls Kakori Lucknow best sexual service Online  โ˜‚๏ธCALL ON โžฅ8923113531 ๐Ÿ”Call Girls Kakori Lucknow best sexual service Online  โ˜‚๏ธ
CALL ON โžฅ8923113531 ๐Ÿ”Call Girls Kakori Lucknow best sexual service Online โ˜‚๏ธ
ย 
CHEAP Call Girls in Pushp Vihar (-DELHI )๐Ÿ” 9953056974๐Ÿ”(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )๐Ÿ” 9953056974๐Ÿ”(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )๐Ÿ” 9953056974๐Ÿ”(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )๐Ÿ” 9953056974๐Ÿ”(=)/CALL GIRLS SERVICE
ย 
call girls in Vaishali (Ghaziabad) ๐Ÿ” >เผ’8448380779 ๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธ
call girls in Vaishali (Ghaziabad) ๐Ÿ” >เผ’8448380779 ๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธcall girls in Vaishali (Ghaziabad) ๐Ÿ” >เผ’8448380779 ๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธ
call girls in Vaishali (Ghaziabad) ๐Ÿ” >เผ’8448380779 ๐Ÿ” genuine Escort Service ๐Ÿ”โœ”๏ธโœ”๏ธ
ย 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
ย 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
ย 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
ย 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
ย 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlanโ€™s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlanโ€™s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlanโ€™s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlanโ€™s ...
ย 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
ย 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
ย 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
ย 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
ย 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
ย 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
ย 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
ย 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
ย 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
ย 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
ย 

5 surprising oracle sql behaviors that very few people know

  • 1. 5 Surprising Oracle SQL Behaviors That Very Few People Know
  • 2. Question #1 for an Oracle SQL expert Which of the queries below contain the rows with Null Salary? SELECT * FROM Persons WHERE SALARY > 1000 SELECT * FROM Persons WHERE SALARY <= 1000
  • 3. Answer In fact, the conditions both implicitly assume that SALARY is not NULL. SALARY > 1000 and SALARY <= 1000 None of them
  • 4. All logical operators are FALSE when one operand is NULL NULL = 1 NULL != 1 NULL > 1 NULL = "ABC" NULL != "ABC" NULL LIKE "Steve%" NULL NOT LIKE "Steve%" NULL = NULL All the below examples return FALSE
  • 5. How do we get the rows where SALARY is NULL? We use the operator IS NULL SELECT * FROM Persons WHERE SALARY IS NULL
  • 6. How to check whether a value is Null or Not? Recall from earlier, the below conditions are always FALSE โ€ข COL = NULL โ€ข COL != NULL Use the below operators instead โ€ข IS NULL โ€ข IS NOT NULL
  • 7. Question #2 for an Oracle SQL expert Which condition detects a zero-length character field? WHERE COL = โ€˜โ€™ WHERE Length(COL) = 0
  • 9. Explanations โ€ข In Oracle character fields cannot be of zero length. โ€ข Oracle substitutes NULL to a โ€˜โ€™ literal in Queries โ€ข When we insert a โ€˜โ€™ in a cell , NULL gets inserted โ€ข The condition X = โ€˜โ€™ is interpreted as X = NULL -> a condition that is always false.
  • 10. โ€ข So, to test if a Col is empty, we test if itโ€™s null: โ€ข Length(COL) = 0 does not work, since Length(NULL) is NULL. โ€ข The correct condition based on length is: โ€ข So, itโ€™s more concise to use the first condition. WHERE COL IS NULL WHERE Length(COL) IS NULL
  • 11. Question#3 for an Oracle SQL expert What does the queries below do? SELECT NAME, age, weight / ( height * height ) AS BMI FROM persons ORDER BY BMI SELECT NAME, age, weight / ( height * height ) AS BMI FROM persons WHERE BMI > 25
  • 12. Answer They both raise errors Oracle does not accept using a column alias in Where or Order by clauses.
  • 13. Solution 1 Repeat the definitions of the aliases when needed SELECT NAME, age, weight / ( height * height ) AS BMI FROM persons ORDER BY weight / ( height * height) SELECT NAME, age, weight / ( height * height ) AS BMI FROM persons WHERE weight / ( height * height) > 25 Cons: โ€ข Too much redundancy. โ€ข Query is too long. โ€ข Every time we change one alias, we must modify it definition in several locations.
  • 14. Solution 2 This method is generic and works in all cases SELECT * FROM (SELECT NAME, age, weight / (height * height) AS BMI FROM PERSONS) WHERE BMI > 25 SELECT * FROM (SELECT NAME, age, weight / (height * height) AS BMI FROM PERSONS) ORDER BY BMI The part between parentheses acts as a new temporary table, that now has a column named after each alias. Now, we can use the aliases in the WHERE and ORDER BY expressions.
  • 15. Question #4 for an Oracle SQL expert What does the following requests return? SELECT ColX, * from MYTABLE SELECT ColX, * , ColZ from MYTABLE
  • 16. Answer Oracle, only accepts the asterisk when itโ€™s the only thing between the Select and From, as in: Select * from MYTABLE If we mix, columns with asterisk, (as in the queries below) Oracle will not understand the syntax and will raise errors. Select ColX, * from MYTABLE Select ColX, *, ColZ from MYTABLE
  • 17. Good news! Oracle will accept the syntax if we simply prefix the asterisk symbol with the table name (or table alias name), as shown below: Select colX, MYTABLE.* from MYTABLE Select colX, m.* from MYTABLE m
  • 18. Question #5 for an Oracle SQL expert Which of the below queries gets the top N rows from a table? Select TOP N * from MYTABLE Select * from MYTABLE LIMIT 10
  • 19. Answer โ€ข They both fail. โ€ข Syntax 1 works on SQL Server โ€ข Syntax 2 works on MySQL โ€ข On Oracle, we must use the pseudo-column rownum, that is a virtual column that represents the line number. Select * from MYTABLE where ROWNUM <=N
  • 20. โ€ข So, if we want to get the first 3 rows, we can use: โ€ข Rownum can also be included in the list of fields, allowing us to display the number of each row: Select * from MYTABLE where rownum < 4 Select rownum, MYTABLE.* from MYTABLE where rownum < 4
  • 21. Beware of rownum pitfall #1 Remember that filtering is executed before the sorting will sort the list containing the first 3 students in the table - Not very useful. The proper way to get the top N rows of a sorted query is to use an inline view as shown below: Select Name, Grade from Students where rownum < 4 order by grade SELECT * FROM (SELECT Name, Grade FROM Students ORDER BY Grade) WHERE ROWNUM < 4
  • 22. Beware of rownum pitfall 2 The first row that breaks the condition on rownum will stop the execution of the query. The below conditions, for example, will yield no results since they fail on the first row of the table. ROWNUM > 1 ROWNUM = 3 There is a workaround for this limitation by using an inline view containg rownum. Select * from (Select ROWNUM AS lineNb, mytabe.* from mytable WHERE ROWNUM < 4) WHERE lineNb = 3