0% found this document useful (0 votes)
15 views17 pages

Notes Xi List

The document provides an overview of Python lists, including their mutability, types, indexing, and various functions for manipulating lists such as append, extend, insert, and remove. It also covers basic SQL concepts, including primary keys, foreign keys, and commands related to data manipulation and definition. Additionally, it discusses arithmetic operations and the use of distinct in SQL queries.

Uploaded by

Anon res antinio
Copyright
© © All Rights Reserved
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)
15 views17 pages

Notes Xi List

The document provides an overview of Python lists, including their mutability, types, indexing, and various functions for manipulating lists such as append, extend, insert, and remove. It also covers basic SQL concepts, including primary keys, foreign keys, and commands related to data manipulation and definition. Additionally, it discusses arithmetic operations and the use of distinct in SQL queries.

Uploaded by

Anon res antinio
Copyright
© © All Rights Reserved
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
You are on page 1/ 17

LIST

-----------------------------------------------------------------------------------
--------------------------------------------------------------------------
list are mutable sequence of python
mutable = changable
lists are collection of elements which can store into the single variable, the
lists are depicted through square brackets [],
e.g
a = 10
a = [10]
a = [10,9,8,7]
-----------------------------------------------------------------------------------
--------------------------------------------------------------------------
TYPES OF LIST:
1.Empty list
li = []

2.Number list:
li = [1,2,3,4,5,6,7,8]
(only whole numbers no decimal)

3.Decimal list
li = [1.2,2.56,5.67]

4.String/test/character list:
li = ['name_1','name_2','name_3']

5.Mixed List
li = [1101,'name',23.32] (heterogenious)

- A list can contain heterogenious types of data...


-----------------------------------------------------------------------------------
--------------------------------------------------------------------------
#index number

- to use a single element from a list

say
li = [1,2,3,4,5]

[1][2][3][4][5]
index number = 0 1 2 3 4

- index number always starts with 0

Syntax:
print (li[index_n0.])

e.g.
li = [1,2,3,4,5] Output: 1
print (li[0])
print (li[1]) Output: 2

[1] [2] [3] [4] [5]


li[0] li[1] li[2] li[3] li[4]

- index number can be negative


e.g.
print (li[-1]) output: 5

-5 -4 -3 -2 -1 <------- (not zero beacuse zero cannot be negative)


[1][2][3][4][5]
index number = 0 1 2 3 4

- for more than one element


syntax:
print (li[index_1,index_2])

e.g.
li = [1,2,3,4,5]
print (li[1:4]) Output: [2 , 3 , 4]

e.g.
li2=['a','e','i','o','u']
print (li2)
print(li2[2])
li2[2]='z'
print (li2)

-value and size are mutable in list.

-----------------------------------------------------------------------------------
--------------------------------------------------------------------------

*FUNCTIONS

- functions are set of statement with some specific work

*TYPES OF FUNCTIONS

1.LIBRARY FUNCTION
- it is also known as built i or ready made function.

2.USER DEFINED FUNCTION


- it is made according to the user's will/requirement
-in a function there shoulld be an append() and, if it is not there then it is no a
function

#.append() - add at the very end

[23][34][67][78]{234}{345}
^ ^
| |

appended elements
#.extend() - same as append()

Syntax: listname.append(elemnts to add at the very end)


###e.g.{append}
li = [1,2,3,4,5]
li.append([90,56])
print (li)

output: [1, 2, 3, 4, 5, [90, 56]]

###e.g.{extend}
li = [1,2,3,4,5]
li.extend([90,56])
print (li)

output: [1, 2, 3, 4, 5, 90, 56]

# difference b/w both


for multiple value when using append(), the value gets alloted the same index_no. ,
whereas in extend() , multiple value get stored in multiple index no.s

li.append([67,78,70])

li = [][][][]{67}{78}{70}
index no. 1 2 3 4 5

li.extend([67,78,90])
li = [][][][]{67}{78}{90}
index no. 1 2 3 4 5 6 7

#insert(_,_)
to add a value as per the list (can add a value in between)

Synatx:
listname.insert(indexno,your value)

###e.g.
li = [1,2,3,4,5]
print (li)

output:[1, 2, 3, 4, 5]

li = [1,2,3,4,5]
li.insert(2,78)
print (li)

output:[1, 2, 78, 3, 4, 5]

*To a delete a value or element in a list


- del <------not a function
- pop()
- clear()
- remove()
[functions end with a "()". Here, 'del' is not a function]

#del
###e.g.
li = [1,2,3,4,5]
del li[3]
print(li)

OUTPUT:[1, 2, 3, 5]

#to delete multiple elements:-


###e.g.
li = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
del li[0:7] <------------------------deletes elements from index no's 0 to 6
print(li)

OUTPUT:
[8, 9, 10, 11, 12, 13, 14, 15]

#pop()
###e.g.
li = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
li.pop(4)
print(li)

OUTPUT: [1, 2, 3, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

###e.g.
li = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
li.pop()
print(li)
#if no index no. is alloted then the last element will get deleted

OUTPUT:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

#clear()
clears the entire list (does not require an attribute in newer versions)

#remove()
delets one or more specified elements
###eg.
li = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
li.remove(7)
print(li)

OUTPUT:
[1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15]

***Binary Operators on a list


2 operands = binary
1 operand = unirary (python only has binary operators)
a+b

a + b
('a' and 'b' are operands)
('+' binary operator)

###e.g.
li = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
li2 = [56,57,58,59,60,61,62,63,64,65]
print (li+li2)
OUTPUT:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 56, 57, 58, 59, 60, 61, 62, 63,
64, 65]

###e.g.
li = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
li2 = [56,57,58,59,60,61,62,63,64,65]
print (li+3)

OUTPUT:
TypeError: can only concatenate list (not "int") to list

Here 3 is an integer and not a list

###e.g.
li = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
li2 = [56,57,58,59,60,61,62,63,64,65]
print (li+li2)
print ("Example 2:")
print (li*3)

Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 56, 57, 58, 59, 60, 61, 62, 63,
64, 65]
Example 2:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

###e.g.
li3 = ['a','b','c','d','e','f']
li4 = ['q','w','e','r','t']
print (li3+li4)
print (li3*4)

OUTPUT:

['a', 'b', 'c', 'd', 'e', 'f', 'q', 'w', 'e', 'r', 't']
['a', 'b', 'c', 'd', 'e', 'f', 'a', 'b', 'c', 'd', 'e', 'f', 'a', 'b', 'c', 'd',
'e', 'f', 'a', 'b', 'c', 'd', 'e', 'f']

***Relational operators in a list


< > <= >= == =!

###e.g.
li3 = ['a','b','c','d','e','f']
li4 = ['q','w','e','r','t']
print (li3>li4)
print (li3>4)

OUPUT:
False
True

(checks individual elements and then compares)

***To print multiple selective elements


print (li[start,stop,step])

start - start value


stop - end value
step - by how many steps to print

e.g.
li = [1,2,3,24,5,65,7,8,96,10,11,12,13,14,15]
print (li[2:9:1])

OUTPUT
[3, 24, 5, 65, 7, 8, 96]
UNIT-II DATA MANAGEMENT
-------------------------

1. Primary key:
a column or set of columns which contains a unique data known as primary key. It
cannot be blank or null value.

2. Candidate key:
all the columns of the table which serve as the primary key are known as candidate
key

3. Alternate key:
the candidate keys which are not acting as primary key are known as alternate keys

4. Foreign Key:
foreign key is a column which is used to relate to or more than two tables, it is
compulsory that a foreign key column in first table should be
primary key in related table.

# REFRENCIAL INTEGRITY
- Its is a system of rules that a dbms uses to ensure the relationship between
records in a related table are valid...

SQL
app name : sql (sequencial query language)

More softwares like this:


1. Oracle
(free of cost)
2. MS SQL (microsoft)
(paid)
3. Fox Pro
4. MS Access
___________________________________________________________________________________
______________
Working mechanism:
(client server architecture)

<----------Request-------------

[ ] [ ]
[ ] [ ]

Server client

------------Response------------->

___________________________
to filter out bad requests
___________________________

[ ] { } [ ] {3 tier
architecture}
[ ] { } [ ]

Server business client


rule

___________________________________________________________________________________
_______________

GUI - graphical user interface


CUI - comand line user interface or character user interface
___________________________________________________________________________________
_______________

Commands:
1. DDL (data defination language)
- to create new things

2. DML (data manupilation language)


- to update,delte,add,manupilate data

3. TCL (transaction control language)


- not in syllabus

arithmetic operations:

ADITTION:
--------

starting:
use managerie

show databases;

show tables;

select from * empl; making table on this querry:

--------------------------------------

select ename, sal % commm from empl;

sal - salary column


comm - comission
+-----------+------------+
| ename | sal % comm |
+-----------+------------+
| SMITH | NULL |
| ANYA | 100.00 |
| SETH | 250.00 |
| MAHADEVAN | NULL |
| MOMIN | 1250.00 |
| BINA | NULL |
| SHIAVNSH | NULL |
| SCOTT | NULL |
| AMIR | NULL |
| KULDEEP | NULL |
| ANOOP | NULL |
| JATIN | NULL |
| FAKIR | NULL |
| MITA | NULL |
+-----------+------------+

here sal % comm is formed from the addition from sal% and comm

---------------------------------------------------------------------
Q# display the name of the student and their updates marks by 5

#mysql> select * from student;


#mysql> select name,marks,marks+5 from student;
+-----------+-------+---------+
| name | marks | marks+5 |
+-----------+-------+---------+
| Abu Bakar | 456 | 461 |
| Aanya | 340 | 345 |
| Gurvinder | 480 | 485 |
| Ali | 260 | 265 |
| Michelle | 321 | 326 |
| Zubin | 412 | 417 |
| Simran | 378 | 383 |
| Fatimah | 400 | 405 |
| Anup | 302 | 307 |
| Mita | 150 | 155 |
+-----------+-------+---------+

#Distinct:
(it is a keyword that can be used to hide duplicate value(s))
List jobs provided in your company:

mysql> show tables;


+---------------------+
| Tables_in_menagerie |
+---------------------+
| dept |
| empl |
| event |
| pet |
| student |
+---------------------+

mysql> select * from empl;


+-------+-----------+-----------+------+------------+---------+---------+--------+
| empno | ename | job | mgr | hiredate | sal | comm | deptno |
+-------+-----------+-----------+------+------------+---------+---------+--------+
| 8369 | SMITH | CLERK | 8902 | 1990-12-18 | 800.00 | NULL | 20 |
| 8499 | ANYA | SALESMAN | 8698 | 1991-02-20 | 1600.00 | 300.00 | 30 |
| 8521 | SETH | SALESMAN | 8698 | 1991-02-22 | 1250.00 | 500.00 | 30 |
| 8566 | MAHADEVAN | MANAGER | 8839 | 1991-04-02 | 2985.00 | NULL | 20 |
| 8654 | MOMIN | SALESMAN | 8698 | 1991-09-28 | 1250.00 | 1400.00 | 30 |
| 8698 | BINA | MANAGER | 8839 | 1991-05-01 | 2850.00 | NULL | 30 |
| 8882 | SHIAVNSH | MANAGER | 8839 | 1991-06-09 | 2450.00 | NULL | 10 |
| 8888 | SCOTT | ANALYST | 8566 | 1992-12-09 | 3000.00 | NULL | 20 |
| 8839 | AMIR | PRESIDENT | NULL | 1991-11-18 | 5000.00 | NULL | 10 |
| 8844 | KULDEEP | SALESMAN | 8698 | 1991-09-08 | 1500.00 | 0.00 | 30 |
| 8886 | ANOOP | CLERK | 8888 | 1993-01-12 | 1100.00 | NULL | 20 |
| 8900 | JATIN | CLERK | 8698 | 1991-12-03 | 950.00 | NULL | 30 |
| 8902 | FAKIR | ANALYST | 8566 | 1991-12-03 | 3000.00 | NULL | 20 |
| 8934 | MITA | CLERK | 8882 | 1992-01-23 | 1300.00 | NULL | 10 |
+-------+-----------+-----------+------+------------+---------+---------+--------+

mysql> select job from empl;


+-----------+
| job |
+-----------+
| CLERK |
| SALESMAN |
| SALESMAN |
| MANAGER |
| SALESMAN |
| MANAGER |
| MANAGER |
| ANALYST |
| PRESIDENT |
| SALESMAN |
| CLERK |
| CLERK |
| ANALYST |
| CLERK |
+-----------+

here theres is a lot of duplicate values...

- to display only unique values we use "distinct" keyword.

mysql> select distinct (job) from empl;


+-----------+
| job |
+-----------+
| CLERK |
| SALESMAN |
| MANAGER |
| ANALYST |
| PRESIDENT |
+-----------+

Q# You have a pet shop, show pet species.

mysql> show tables;


+---------------------+
| Tables_in_menagerie |
+---------------------+
| dept |
| empl |
| event |
| pet |
| student |
+---------------------+

mysql> select * from pet;


+----------+--------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+----------+--------+---------+------+------------+------------+
| Fluffy | Harold | cat | f | 1993-02-04 | NULL |
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
| Fang | Benny | dog | m | 1990-08-27 | NULL |
| Bowser | Diane | dog | m | 1979-08-31 | 1995-07-29 |
| Chirpy | Gwen | bird | f | 1998-09-11 | NULL |
| Whistler | Gwen | bird | NULL | 1997-12-09 | NULL |
| Slim | Benny | snake | m | 1996-04-29 | NULL |
| Puffball | Diane | hamster | f | 1999-03-30 | NULL |
+----------+--------+---------+------+------------+------------+

mysql> select distinct (species) from pet;


+---------+
| species |
+---------+
| cat |
| dog |
| bird |
| snake |
| hamster |
+---------+

# "where"
- where is a cluase or keyword which is used to retrieve or fetch raw specific
data...
(-relational operator/conditional operator)

- "select * from (table_name) where (column_name)[relational


operator](number/condition);"

|start:
|- show databases
|- use menagaerie
|- show tables;
|- select * from (table_name)
Q# to show name of students who got above 400 marks...
- open sql
- use menagarie
- show tables;
- select * from student;
- select * from student where marks>400;
(for students who got 400 or more)
- select * from student where marks >= 400;
(for students who got exactly 400 marks)
- select * from student where marks = 400;

Q# show list of boys in class for a footbal team.


- open sql
- use menagarie
- show tables;
- select * from student;
- select * from student where gender = "M";

# NUll
- empty value

(ifnull)
to replace "null" word with something else so user can understand...

pet table:

+----------+--------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+----------+--------+---------+------+------------+------------+
| Fluffy | Harold | cat | f | 1993-02-04 | NULL |
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
| Fang | Benny | dog | m | 1990-08-27 | NULL |
| Bowser | Diane | dog | m | 1979-08-31 | 1995-07-29 |
| Chirpy | Gwen | bird | f | 1998-09-11 | NULL |
| Whistler | Gwen | bird | NULL | 1997-12-09 | NULL |
| Slim | Benny | snake | m | 1996-04-29 | NULL |
| Puffball | Diane | hamster | f | 1999-03-30 | NULL |
+----------+--------+---------+------+------------+------------+

- select name,species, ifnull (death,'alive') from pet;


+----------+---------+------------------------+
| name | species | ifnull (death,'alive') |
+----------+---------+------------------------+
| Fluffy | cat | alive |
| Claws | cat | alive |
| Buffy | dog | alive |
| Fang | dog | alive |
| Bowser | dog | 1995-07-29 |
| Chirpy | bird | alive |
| Whistler | bird | alive |
| Slim | snake | alive |
| Puffball | hamster | alive |
+----------+---------+------------------------+
(to change name of the column)

- select death,ifnull(death,"alive") as 'status' from pet;


+------------+----------------+
| death | status |
+------------+----------------+
| NULL | alive |
| NULL | alive |
| NULL | alive |
| NULL | alive |
| 1995-07-29 | 1995-07-29 |
| NULL | alive |
| NULL | alive |
| NULL | alive |
| NULL | alive |
+------------+----------------+

# Where (continued)

conditional operators: >, <, >=, <=, <>(not equal) , =

- SYNTAX - select ..... from Table name WHERE column (condional operator) value;

e.g. select * from empl where sal>2000;


+-------+-----------+-----------+------+------------+---------+------+--------+
| empno | ename | job | mgr | hiredate | sal | comm | deptno |
+-------+-----------+-----------+------+------------+---------+------+--------+
| 8566 | MAHADEVAN | MANAGER | 8839 | 1991-04-02 | 2985.00 | NULL | 20 |
| 8698 | BINA | MANAGER | 8839 | 1991-05-01 | 2850.00 | NULL | 30 |
| 8882 | SHIAVNSH | MANAGER | 8839 | 1991-06-09 | 2450.00 | NULL | 10 |
| 8888 | SCOTT | ANALYST | 8566 | 1992-12-09 | 3000.00 | NULL | 20 |
| 8839 | AMIR | PRESIDENT | NULL | 1991-11-18 | 5000.00 | NULL | 10 |
| 8902 | FAKIR | ANALYST | 8566 | 1991-12-03 | 3000.00 | NULL | 20 |
+-------+-----------+-----------+------+------------+---------+------+--------+

# Logical operators (for more than one conditions)


AND OR NOT

e.g.
select * from student where gender = "M" and marks >= 300;
+-----------+------+--------+---------+-------+
| name | age | gender | matric | marks |
+-----------+------+--------+---------+-------+
| Abu Bakar | 15 | M | 9531185 | 456 |
| Zubin | 15 | M | 9531184 | 412 |
| Anup | 12 | M | 9531181 | 302 |
+-----------+------+--------+---------+-------+

#NOT
- Not True = False
- Not False = true
Q. select boys from student table who got more than 400 or 400 marks.

> select * from student;


+-----------+------+--------+---------+-------+
| name | age | gender | matric | marks |
+-----------+------+--------+---------+-------+
| Abu Bakar | 15 | M | 9531185 | 456 |
| Aanya | 13 | F | 9531186 | 340 |
| Gurvinder | 14 | F | 9531187 | 480 |
| Ali | 12 | M | 9531188 | 260 |
| Michelle | 11 | F | 9531188 | 321 |
| Zubin | 15 | M | 9531184 | 412 |
| Simran | 13 | F | 9531183 | 378 |
| Fatimah | 14 | F | 9531182 | 400 |
| Anup | 12 | M | 9531181 | 302 |
| Mita | 11 | F | 9531180 | 150 |
+-----------+------+--------+---------+-------+

> select * from student where gender = "M" and marks >=400;
+-----------+------+--------+---------+-------+
| name | age | gender | matric | marks |
+-----------+------+--------+---------+-------+
| Abu Bakar | 15 | M | 9531185 | 456 |
| Zubin | 15 | M | 9531184 | 412 |
+-----------+------+--------+---------+-------+

Q. From the employ table, show mangers or clerk


mysql> select * from empl;
+-------+-----------+-----------+------+------------+---------+---------+--------+
| empno | ename | job | mgr | hiredate | sal | comm | deptno |
+-------+-----------+-----------+------+------------+---------+---------+--------+
| 8369 | SMITH | CLERK | 8902 | 1990-12-18 | 800.00 | NULL | 20 |
| 8499 | ANYA | SALESMAN | 8698 | 1991-02-20 | 1600.00 | 300.00 | 30 |
| 8521 | SETH | SALESMAN | 8698 | 1991-02-22 | 1250.00 | 500.00 | 30 |
| 8566 | MAHADEVAN | MANAGER | 8839 | 1991-04-02 | 2985.00 | NULL | 20 |
| 8654 | MOMIN | SALESMAN | 8698 | 1991-09-28 | 1250.00 | 1400.00 | 30 |
| 8698 | BINA | MANAGER | 8839 | 1991-05-01 | 2850.00 | NULL | 30 |
| 8882 | SHIAVNSH | MANAGER | 8839 | 1991-06-09 | 2450.00 | NULL | 10 |
| 8888 | SCOTT | ANALYST | 8566 | 1992-12-09 | 3000.00 | NULL | 20 |
| 8839 | AMIR | PRESIDENT | NULL | 1991-11-18 | 5000.00 | NULL | 10 |
| 8844 | KULDEEP | SALESMAN | 8698 | 1991-09-08 | 1500.00 | 0.00 | 30 |
| 8886 | ANOOP | CLERK | 8888 | 1993-01-12 | 1100.00 | NULL | 20 |
| 8900 | JATIN | CLERK | 8698 | 1991-12-03 | 950.00 | NULL | 30 |
| 8902 | FAKIR | ANALYST | 8566 | 1991-12-03 | 3000.00 | NULL | 20 |
| 8934 | MITA | CLERK | 8882 | 1992-01-23 | 1300.00 | NULL | 10 |
+-------+-----------+-----------+------+------------+---------+---------+--------+

mysql> select * from empl where job = "CLERK" OR job = "MANAGER";


+-------+-----------+---------+------+------------+---------+------+--------+
| empno | ename | job | mgr | hiredate | sal | comm | deptno |
+-------+-----------+---------+------+------------+---------+------+--------+
| 8369 | SMITH | CLERK | 8902 | 1990-12-18 | 800.00 | NULL | 20 |
| 8566 | MAHADEVAN | MANAGER | 8839 | 1991-04-02 | 2985.00 | NULL | 20 |
| 8698 | BINA | MANAGER | 8839 | 1991-05-01 | 2850.00 | NULL | 30 |
| 8882 | SHIAVNSH | MANAGER | 8839 | 1991-06-09 | 2450.00 | NULL | 10 |
| 8886 | ANOOP | CLERK | 8888 | 1993-01-12 | 1100.00 | NULL | 20 |
| 8900 | JATIN | CLERK | 8698 | 1991-12-03 | 950.00 | NULL | 30 |
| 8934 | MITA | CLERK | 8882 | 1992-01-23 | 1300.00 | NULL | 10 |
+-------+-----------+---------+------+------------+---------+------+--------+

Q. write query to show dogs who have died?

> select * from pet;


+----------+--------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+----------+--------+---------+------+------------+------------+
| Fluffy | Harold | cat | f | 1993-02-04 | NULL |
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
| Fang | Benny | dog | m | 1990-08-27 | NULL |
| Bowser | Diane | dog | m | 1979-08-31 | 1995-07-29 |
| Chirpy | Gwen | bird | f | 1998-09-11 | NULL |
| Whistler | Gwen | bird | NULL | 1997-12-09 | NULL |
| Slim | Benny | snake | m | 1996-04-29 | NULL |
| Puffball | Diane | hamster | f | 1999-03-30 | NULL |
+----------+--------+---------+------+------------+------------+

> mysql> select * from pet where species = "dog" AND death <> "NULL";
+--------+-------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+--------+-------+---------+------+------------+------------+
| Bowser | Diane | dog | m | 1979-08-31 | 1995-07-29 |
+--------+-------+---------+------+------------+------------+

> select * from pet;


+----------+--------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+----------+--------+---------+------+------------+------------+
| Fluffy | Harold | cat | f | 1993-02-04 | NULL |
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
| Fang | Benny | dog | m | 1990-08-27 | NULL |
| Bowser | Diane | dog | m | 1979-08-31 | 1995-07-29 |
| Chirpy | Gwen | bird | f | 1998-09-11 | NULL |
| Whistler | Gwen | bird | NULL | 1997-12-09 | NULL |
| Slim | Benny | snake | m | 1996-04-29 | NULL |
| Puffball | Diane | hamster | f | 1999-03-30 | NULL |
+----------+--------+---------+------+------------+------------+

mysql> select * from pet where species = "dog" and death is not null;
+--------+-------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+--------+-------+---------+------+------------+------------+
| Bowser | Diane | dog | m | 1979-08-31 | 1995-07-29 |
+--------+-------+---------+------+------------+------------+

Q. select salesman who get commision

select * from empl;


+-------+-----------+-----------+------+------------+---------+---------+--------+
| empno | ename | job | mgr | hiredate | sal | comm | deptno |
+-------+-----------+-----------+------+------------+---------+---------+--------+
| 8369 | SMITH | CLERK | 8902 | 1990-12-18 | 800.00 | NULL | 20 |
| 8499 | ANYA | SALESMAN | 8698 | 1991-02-20 | 1600.00 | 300.00 | 30 |
| 8521 | SETH | SALESMAN | 8698 | 1991-02-22 | 1250.00 | 500.00 | 30 |
| 8566 | MAHADEVAN | MANAGER | 8839 | 1991-04-02 | 2985.00 | NULL | 20 |
| 8654 | MOMIN | SALESMAN | 8698 | 1991-09-28 | 1250.00 | 1400.00 | 30 |
| 8698 | BINA | MANAGER | 8839 | 1991-05-01 | 2850.00 | NULL | 30 |
| 8882 | SHIAVNSH | MANAGER | 8839 | 1991-06-09 | 2450.00 | NULL | 10 |
| 8888 | SCOTT | ANALYST | 8566 | 1992-12-09 | 3000.00 | NULL | 20 |
| 8839 | AMIR | PRESIDENT | NULL | 1991-11-18 | 5000.00 | NULL | 10 |
| 8844 | KULDEEP | SALESMAN | 8698 | 1991-09-08 | 1500.00 | 0.00 | 30 |
| 8886 | ANOOP | CLERK | 8888 | 1993-01-12 | 1100.00 | NULL | 20 |
| 8900 | JATIN | CLERK | 8698 | 1991-12-03 | 950.00 | NULL | 30 |
| 8902 | FAKIR | ANALYST | 8566 | 1991-12-03 | 3000.00 | NULL | 20 |
| 8934 | MITA | CLERK | 8882 | 1992-01-23 | 1300.00 | NULL | 10 |
+-------+-----------+-----------+------+------------+---------+---------+--------+

mysql> select * from empl where comm is not null;


+-------+---------+----------+------+------------+---------+---------+--------+
| empno | ename | job | mgr | hiredate | sal | comm | deptno |
+-------+---------+----------+------+------------+---------+---------+--------+
| 8499 | ANYA | SALESMAN | 8698 | 1991-02-20 | 1600.00 | 300.00 | 30 |
| 8521 | SETH | SALESMAN | 8698 | 1991-02-22 | 1250.00 | 500.00 | 30 |
| 8654 | MOMIN | SALESMAN | 8698 | 1991-09-28 | 1250.00 | 1400.00 | 30 |
| 8844 | KULDEEP | SALESMAN | 8698 | 1991-09-08 | 1500.00 | 0.00 | 30 |
+-------+---------+----------+------+------------+---------+---------+--------+

Q.show male dogs and cats.

mysql> select * from pet;


+----------+--------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+----------+--------+---------+------+------------+------------+
| Fluffy | Harold | cat | f | 1993-02-04 | NULL |
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
| Fang | Benny | dog | m | 1990-08-27 | NULL |
| Bowser | Diane | dog | m | 1979-08-31 | 1995-07-29 |
| Chirpy | Gwen | bird | f | 1998-09-11 | NULL |
| Whistler | Gwen | bird | NULL | 1997-12-09 | NULL |
| Slim | Benny | snake | m | 1996-04-29 | NULL |
| Puffball | Diane | hamster | f | 1999-03-30 | NULL |
+----------+--------+---------+------+------------+------------+

mysql> select * from pet where (species = "dog" or species = "cat") and sex = "m" ;
+--------+-------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+--------+-------+---------+------+------------+------------+
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Fang | Benny | dog | m | 1990-08-27 | NULL |
| Bowser | Diane | dog | m | 1979-08-31 | 1995-07-29 |
+--------+-------+---------+------+------------+------------+

#SYMBOLIC REPRESNTATION
OR = ||
AND = &&

# NOT Operator

select * from pet where (species = "dog" or species = "cat") && sex = "m" ;
+--------+-------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+--------+-------+---------+------+------------+------------+
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Fang | Benny | dog | m | 1990-08-27 | NULL |
| Bowser | Diane | dog | m | 1979-08-31 | 1995-07-29 |
+--------+-------+---------+------+------------+------------+
3 rows in set (0.00 sec)

mysql> select * from pet where not (species = "dog" or species = "cat") && sex =
"m" ;
+------+-------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+------+-------+---------+------+------------+-------+
| Slim | Benny | snake | m | 1996-04-29 | NULL |
+------+-------+---------+------+------------+-------+

You might also like