LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-07-2023, 06:56 AM   #1
picklehead
Member
 
Registered: Jun 2018
Posts: 61

Rep: Reputation: Disabled
Simple SQL question (well, not for me)


I need to find the number of rows in a mySQL table where FirstName contains the letter "e" and lastName has more than 5 characters.

Say the table is called Employees or whatever.

TIA
 
Old 02-07-2023, 06:58 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,345
Blog Entries: 3

Rep: Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756
Yes, it's simple but what have you tried so far and where are you stuck? And which variant of SQL is this on, MariaDB, PostgresSQL, or SQLite3?
 
Old 02-07-2023, 07:00 AM   #3
picklehead
Member
 
Registered: Jun 2018
Posts: 61

Original Poster
Rep: Reputation: Disabled
The output should have format kind of like this:

------------------------------------
| count(*) |
------------------------------------
| 2 |
------------------------------------

NOTE: Can't format but it should be maybe like a box with count(*) in the top box and the actual count number below it.

Last edited by picklehead; 02-07-2023 at 07:03 AM.
 
Old 02-07-2023, 07:17 AM   #4
picklehead
Member
 
Registered: Jun 2018
Posts: 61

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Turbocapitalist View Post
Yes, it's simple but what have you tried so far and where are you stuck? And which variant of SQL is this on, MariaDB, PostgresSQL, or SQLite3?
I haven't tried anything yet. I don't even know what SQL command to use because I know nothing about SQL. I don't even have a DB on my CentOS box. Is there a resource online where you can practice SQL commands?
 
Old 02-07-2023, 07:24 AM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,345
Blog Entries: 3

Rep: Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756
Yes, the quick and very easy way is to install SQLite3. It is the least complicated. If you have a RPM derivative,

Code:
sudo yum update
sudo yum install sqlite3
Then you can access it in a terminal by pointing the interactive client at a file which will serve as you database:

Code:
sqlite3 /home/me/Documents/my.sql.database.sqlite3
The manual for SQLite is quite good and there are also lots of good (and bad) tutorials out there.

The first thing to do after that would be to create a table with the appropriate fields (aka columns). Then fill the table with a few sample records (aka rows) with data. Then you can try various searches.
 
Old 02-07-2023, 07:27 AM   #6
picklehead
Member
 
Registered: Jun 2018
Posts: 61

Original Poster
Rep: Reputation: Disabled
OK so I don't want to come off like a complete helpless case so would it be something like this:

SELECT COUNT(*)
FROM Employees
WHERE FirstName CONTAINS "e" AND LastName > 5

Is my syntax right or will that produce gobbledygook or just bork?
 
Old 02-07-2023, 07:31 AM   #7
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,345
Blog Entries: 3

Rep: Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756
What does the table which you've created look like? And do you have a few rows of fake data yet?
 
Old 02-07-2023, 07:42 AM   #8
picklehead
Member
 
Registered: Jun 2018
Posts: 61

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Turbocapitalist View Post
What does the table which you've created look like? And do you have a few rows of fake data yet?
I haven't created a table. This is hypothetical. It's a possible technical interview question. Let's just assume that it's a basic table with a list of people's name, so the will be a FirstName column, a LastName column. There might be a few other columns like Age or HairColour. We are only interested in finding the number of entries where the FirstName contains letter "e" and Lastname has more than 5 characters.


So if we have

Firstname Lastname

eddie pillar
jo spencer
eve potter
joe bloggs




The answer is three.
 
Old 02-07-2023, 07:47 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
Turbocapitalist has provided a link that could help you understand SQL and solve this question
 
Old 02-07-2023, 08:13 AM   #10
picklehead
Member
 
Registered: Jun 2018
Posts: 61

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
Turbocapitalist has provided a link that could help you understand SQL and solve this question
I don't want to sound ungrateful but that link has wads of documentation and I don't see any examples, like you get in O'Reilly books.
It'll take me weeks to trawl through that. But thanks anyway.
I don't see anything in the Syntax that allows you to count characters in an entry.
 
Old 02-07-2023, 08:23 AM   #11
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,345
Blog Entries: 3

Rep: Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756
You're getting the sequence wrong. You need to set up a table with few rows of data first, so you have something to search in. e.g. https://www.w3schools.blog/create-table-sqlite
 
Old 02-07-2023, 08:29 AM   #12
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,623

Rep: Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555

If you want to succeed in a technical interview that involves answering SQL questions, you should know basic SQL.

A good way to learn basic SQL is to setup a dummy database and run queries against it.

That way, you don't ask questions like "will this query work" you run it yourself and find out.


Last edited by boughtonp; 02-07-2023 at 08:32 AM.
 
1 members found this post helpful.
Old 02-07-2023, 08:30 AM   #13
picklehead
Member
 
Registered: Jun 2018
Posts: 61

Original Poster
Rep: Reputation: Disabled
OK so I figured out how to find records where FirstName contains letter "e"

SELECT FirstName,LastName FROM Employees
WHERE FirstName LIKE '%e%'


But I have no idea how to work out how to count characters in a field.
 
Old 02-07-2023, 08:36 AM   #14
picklehead
Member
 
Registered: Jun 2018
Posts: 61

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by boughtonp View Post
If you want to succeed in a technical interview that involves answering SQL questions, you should know basic SQL.

A good way to learn basic SQL is to setup a dummy database and run queries against it.

I agree, but this isn't a DBA or SQL job. It's a Linux Team job and there's only this one SQL question.
I will get an interview if I can answer this banana skin question on the technical assessment.

All my other skills Linux, SNMP, Networking, bash scripting, virtualisation are fine. It's just this one question that some guy recommend they ask and it's completely irrelevant to the job but they've lost loads of candidates because of it. Just because of pettiness.
 
Old 02-07-2023, 08:36 AM   #15
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,623

Rep: Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555
Quote:
Originally Posted by picklehead View Post
But I have no idea how to work out how to count characters in a field.
This will usually be listed in the documentation under "string functions".

(For Sqlite, it is listed under "scalar functions".)

 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Moving with SQL Server to Linux? Move from SQL Server to MySQL as well! LXer Syndicated Linux News 0 11-28-2016 02:02 PM
[SOLVED] Graphics acceleration works well for root, not so well for user lipk Slackware 3 03-28-2013 04:16 AM
SQL noob question: can SQL effectively handle a 2D matrix? JoeyAdams Programming 2 03-17-2008 01:10 AM
SQL: old sql dump not loading into new db BrianK Linux - Software 1 07-25-2006 11:55 PM
Well well well..what do we have here? DaBlade Linux - News 4 10-03-2005 10:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration