Selecting All Fields

To display all fields of all persons in the Person table, you can use the following query: SQL

SELECT * 
FROM Person;

Pandas

result = person

Selecting First Names and Last Names**

To obtain only the first names and last names of each person, use this query: SQL

SELECT Name, Surname 
FROM Person;

Pandas

result = person[["Name", "Surname"]]

Alias for the Table

You can use an alias to simplify your queries. For example, using p as an alias for Person:

SELECT p.Name, p.FiscalCode 
FROM Person AS p;

Distinct

To select a list of unique first names from the Person table, you can use: SQL

SELECT DISTINCT Name 
FROM Person;

This selects the list of first names of persons without duplicates. Pandas In pandas we will use the unique() method.

result = person[["Name", "Surname"]].unique()

It is also possible to use DISTINCT within aggregate functions.

-- Count all persons who have a name (Name <> NULL)
SELECT COUNT(Name)
FROM Person;
 
-- Count the total number of distinct names
SELECT COUNT(DISTINCT Name)
FROM Person;

Select nth row

To select the nth row of a table, we can use the LIMIT clause: SQL

SELECT *
FROM Employee
LIMIT 1 OFFSET 2 -- 3rd element (2nd index)

Pandas In pandas, we can use iloc[]

df = employee.iloc[n]

tags: databases SQL