> ## Documentation Index
> Fetch the complete documentation index at: https://rajanand.org/llms.txt
> Use this file to discover all available pages before exploring further.

# SQL Server beginner interview questions and answers

> Begineer Interview Q&A

### How to generate a random string of 10 character?

There are many ways to generate a random string. This one is a simple way to do it.

```sql theme={"system"}
SELECT RIGHT(CAST(NEWID() AS VARCHAR(255)),10)
```

### How to generate a random number?

Generate a random number from 0 through 1, exclusive.

```sql theme={"system"}
SELECT RAND()
```

Generate a random number between 0 and 100.

```sql theme={"system"}
SELECT FLOOR(RAND()*101)
SELECT CAST(RAND()*100 AS INT)
```

Generate a random number between 1 and 100

```sql theme={"system"}
SELECT CEILING(RAND()*100)
```

Generate a random number between 10 and 30

```sql theme={"system"}
SELECT 10 + FLOOR(RAND()*30)
```

### COUNT(\*) vs COUNT(1) vs SUM(1)

#### What is the output of the below queries when table is empty and non-empty?

```sql theme={"system"}
SELECT COUNT(*) FROM STUDENT
SELECT COUNT(1) FROM STUDENT
SELECT SUM (1) FROM STUDENT
```

When the table is empty:

<Frame>
  ![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1651956959478/meBznhe6m.png)
</Frame>

When the table is non-empty:

<Frame>
  ![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1651956952276/3vtCTSlJl.png)
</Frame>

### How to select all the even number records from a table?

There is a table `participant` which has more than 10k records and you need to select all the records with id in even number (i.e. 2,4,6,8 etc)

<Frame>
  ![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1652941086947/7sCKuXM3O.png)
</Frame>

```sql theme={"system"}
SELECT * FROM dbo.participant WHERE id % 2 = 0
```

### How to select all the odd number records from a table?

There is a table `participant` which has more than 10k records and you need to select all the records with id in odd number (i.e. 1,3,5,7 etc)

<Frame>
  ![screenshot\_001037\_2022\_05\_19\_11\_47\_19.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1652941001540/DIIWTrp_I.png)
</Frame>

```sql theme={"system"}
SELECT * FROM dbo.participant WHERE id % 2 = 1
```

### How to select all the records from the table except the id's which are divisible by 3?

There is a table `participant` which has more than 10k records and you need to select all the records from the table except id in 3,6,9, etc

<Frame>
  ![screenshot\_001036\_2022\_05\_19\_11\_42\_56.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1652941047195/vz0Y-pyQe.png)
</Frame>

```sql theme={"system"}
SELECT * FROM dbo.participant WHERE id % 3 <> 0
```
